0

I have the following data:

  var data = { 
    someData : [
      {
        title: "something",
        color: "red",
        moreInfo : [
          {
            title: "something else",
            color: "orange",
            moreInfo : [
              {
                title: "something more",
                color: "green",
                moreInfo : [
                  {
                    title: "another title",
                    color: "yellow"
                  }
                ]
              }
            ]
          },
          {
            title: "blah blah",
            color: "blue"
          }
        ]
      },
      {
        title: "something cool",
        color: "black"
      }
    ]
  };

I want to run a function to return the object with the property that matches a certain value. Currently I have this:

  var result = data.filter(obj => {
    return obj.title === "something more";
  });

I want the function to be able to iterate through every object and nested object and return the object with that value. So in this case I would like for it to return:

{
  title: "something more",
  color: "green",
  moreInfo: [
    {
     title: "another title",
     color: "yellow"
    }
 ]
}

If someone could please help me with this. It seems simple enough but I have spent way too much time on this.Thank you!

3
  • 1
    Have you tried to implement a solution using recursion? Commented Feb 28, 2020 at 2:59
  • @fubar thanks for responding. I have but I'm having trouble returning the object if it is nested. Commented Feb 28, 2020 at 3:09
  • 1
    There is an error in your code. The error is: Uncaught SyntaxError: Invalid shorthand property initializer! somedata = [] it should be somedata : [] Commented Feb 28, 2020 at 3:10

2 Answers 2

2

You can filter array recursively using below-mentioned function:

  var filter = (array) => {
    array.filter(obj => {
     if(obj.title === "something more"){
     return obj     
   }
    if(obj.moreInfo){
     filter(obj.moreInfo)
    }
    });
  } 
Sign up to request clarification or add additional context in comments.

Comments

0

Use a recursive function like this:

function dig(obj, func){
  let v;
  if(obj instanceof Array){
    for(let i=0,l=obj.length; i<l; i++){
      v = obj[i];
      if(typeof v === 'object'){
        dig(v, func);
      }
      else{
        func(v, i, obj);
      }
    }
  }
  else{
    for(let i in obj){
      v = obj[i];
      if(typeof v === 'object'){
        dig(v, func);
      }
      else{
        func(v, i, obj);
      }
    }
  }
}
// I don't like your sytax - but didn't fix it
var data = { 
    someData: [
      {
        title: "something",
        color: "red",
        moreInfo: [
          {
            title: "something else",
            color: "orange",
            moreInfo: [
              {
                title: "something more",
                color: "green",
                moreInfo: [
                  {
                    title: "another title",
                    color: "yellow"
                  }
                ]
              }
            ]
          },
          {
            title: "blah blah",
            color: "blue"
          }
        ]
      },
      {
        title: "something cool",
        color: "black"
      }
    ]
  };
function getObjByTitle(object, title){
  let obj = false;
  dig(object, (v, k, o)=>{
    if(k === 'title' && v === title){
      obj = o;
    }
  });
  return obj;
}
console.log(getObjByTitle(data, 'something more'));

Comments

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.