• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to create array of object in javascript explicitly

Resolved: How to create array of object in javascript explicitly

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I receive a file path string like so:
And I want to convert this into the following structure dynamically. How should I best go about this?

Answer:

split on / and then build it from there – you can do this concisely with map:


This is very simple – just set the id property to be whatever value you’re currently iterating over, and then if there exists a value before it (i.e. if this is not the first/root item) then set its parent value to the previous value; otherwise, it’s an empty string. You could remove componentsArr and refer to the splitting n times but that’s mildly inefficient in my opinion.

If you have better answer, please add a comment about this, thank you!

arrays javascript object path
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How do I use SetWindowText with Unicode in Win32 using PowerShell?

01/04/2023

Resolved: Shopware 400 Status Error “This value is too long. It should have 255 character or less.” When I Try Updating Database Table

01/04/2023

Resolved: Using AWK to count multiple patterns in a file

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.