-1

I'd like to pass as an input the name of a folder that I want to search for, and get as an output the object that it belongs to.

My array is like this:

const array = {
  item: [{
      name: "parentFolder1",
      item: [{
          name: "subFolder1",
          item: []
        },
        {
          name: "subFolder2",
          item: []
        }
      ]
    },
    {
      name: "parentFolder2",
      item: [{
          name: "sub1",
          item: []
        },
        {
          name: "sub2",
          item: []
        }
      ]
    }
  ]
};


const sub = Object.values(array).map(x =>
  x.find(y => y.item.find(obj => obj.name = "sub2")))

console.dir(sub)

The output I want:

    {
      name: "sub2",
      item: []
    }

The output I get:

[ { name: 'parentFolder1', item: [ [Object], [Object] ] } ]
karel
  • 5,489
  • 46
  • 45
  • 50
brxnzaz
  • 481
  • 1
  • 4
  • 15
  • Do you mean output instead of input? – sudo Mar 12 '19 at 10:26
  • sorry, it was a mistake. i corrected it – brxnzaz Mar 12 '19 at 10:28
  • You are doing an affectation with `obj.name = "sub2"`. You neeed to use the comparison operator : `obj.name === "sub2"` – Seblor Mar 12 '19 at 10:31
  • 2
    Possible duplicate of [JavaScript recursive search in JSON object](https://stackoverflow.com/questions/22222599/javascript-recursive-search-in-json-object) – Elie Nassif Mar 12 '19 at 10:32
  • @Seblor i get the same result – brxnzaz Mar 12 '19 at 10:32
  • 1
    @brxnzaz no, it returns "parentFolder2" instead of "parentFolder1" (even if you got the same result, it was an error you would had to fix). Also, `Array.find` is not recursive. Look at the other answers the people are linking. – Seblor Mar 12 '19 at 10:33

2 Answers2

2

const array = {
  item: [{
      name: "parentFolder1",
      item: [{
          name: "subFolder1",
          item: []
        },
        {
          name: "subFolder2",
          item: []
        }
      ]
    },
    {
      name: "parentFolder2",
      item: [{
          name: "sub1",
          item: []
        },
        {
          name: "sub2",
          item: []
        }
      ]
    }
  ]
};


const val = array.item.map(folder => folder.item.find(obj => obj.name === "sub2")).find(val => val)
console.log(val)
AlexOwl
  • 869
  • 5
  • 11
0

If you need it to be recursive

function find(name, obj) {
  if(obj.name === name) return obj;
  return obj.item.reduce((result, item) => result || find(name, item), false)
}

const array = {
  item: [{
      name: "parentFolder1",
      item: [{
          name: "subFolder1",
          item: []
        },
        {
          name: "subFolder2",
          item: [{
            name: "subsub",
            item: [{
              name: "searchme",
              item: []
            }]
          }]
        }
      ]
    },
    {
      name: "parentFolder2",
      item: [{
          name: "sub1",
          item: []
        },
        {
          name: "sub2",
          item: []
        }
      ]
    }
  ]
};


console.log(find("searchme", array))
AlexOwl
  • 869
  • 5
  • 11