2

There is data returned from server containing an array of strings as hierarchy like this:

var array = [
 "house.bedroom.bed",
 "house.kitchen.spoon",
 "house.kitchen.knife",
 "house.bedroom.sofa",
 "house.bedroom.tv",
 "plants.trees",
 "house.birds.parrot.grey"
 ]

i have successful made a tree data structure as object out of it to make Output the data in tree form below:

  house
    bedroom
      bed
      sofa
      tv
    kitchen
      spoon
      knife
    birds
      parrot
        grey
  plants
    trees

and is there any way to pick a string? for example of asked "kitchen" i want to return all related to that string like this:

house.kitchen.knife
house.kitchen.spoon

Here the codes that i learned:

function find([key, values], string, temp = []) {
    var result;
    temp = temp.concat(key);
    if (key === string) {
        return temp.slice(1).join('.');
    }
    values.some(a => result = find(a, string, temp));
    return result;
}


var result = array.reduce((r, s) => {
        ('root.' + s).split('.').reduce((a, item) => {
            var array = a.find(([v]) => v === item);
            if (!array) {
                a.push(array = [item, []]);
            }
            return array[1];
        }, r);
        return r;
    }, []).pop();

console.log(find(result, 'kitchen'));
console.log(result);

my output is:

house.kitchen
4
  • I think you just need to filter source array array.filter((path) => path.split('.').includes('kitchen')) Commented Jan 13, 2022 at 3:24
  • i tried that it worked perfect but the goal is i need to turn the array = [] output to object to satisfy the tree data structure and then i apply find() function to find match parent and children related "kitchen"........ i made the tree but not able to run find() to match parent and children of "kitchen" to satisfy expected output Commented Jan 13, 2022 at 3:28
  • You have a function that make a tree from an array. Just make the tree from the filtered array. Commented Jan 13, 2022 at 3:37
  • Does this answer your question? array of strings to tree data structure Commented Apr 11, 2023 at 14:29

2 Answers 2

1

I propose to filter the original array

const data = ["house.bedroom.bed","house.kitchen.spoon", "house.kitchen.knife","house.bedroom.sofa","house.bedroom.tv",
 "plants.trees","house.birds.parrot.grey"];
 
 
 const result = data.filter((path) => path.split('.').includes('kitchen'));
 
 console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Sign up to request clarification or add additional context in comments.

1 Comment

actually this solution is helpful but i will need to research more to print out same result of your output from my tree data sctructure not from given data file
0

I believe I understand what you're asking. I would solve this problem with recursion.

function parse(items) {
   return items.reduce((acc, item) => {
      const k = item.slice(0, item.indexOf('.'))
      const v = item.slice(item.indexOf('.') + 1).split('.')
      
      const newItem = {
        [k]: v.length > 1 ? parse(v) : v
      }

      return Object.assign(acc, newItem)
   }, { })
}

This is not a complete solution but should get the general idea across. For each item in the array, split it off into a key and a value. The key will be the string before the first ., and the value with either be the single string after that ., or an object containing children.

3 Comments

hi.. thank you for your help. how should i set this recursion???
what you are asking is almost a backtracking solution. There is no relative ordering to the elements in the array and this would only complicate things. A simple scan through the array should work better. Or you could modify your tree object to include the complete path at the leaf level.
I imagine this could be made to be very simple with some effort. You could be right, but this to me seems like a great opportunity for recursion because the problem is fractal. What was done for "house.bedroom.bed" must be done again for "bedroom.bed".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.