1

I'm trying return an object from the code below that has the key value pair of name:sparky and return the entire metadata and stats array for that object.

I don't want to use Object.values(objectArray)[0] because this data is coming from an API and I expect the objects position in the array to change in the future.

I've tried objectArray.find but I don't know how to use that to find a value of an array which is inside another array. The value for name will always be unique and the actual objectArray has many more objects inside of it.

Help would be greatly appreciated!

Code

objectArray = [
  {
    "metadata": [
      {
        "key": '1',
        "name": "sparky"
      }
    ],
    "stats": [
      {
        "statsFieldOne": "wins"
      },
      {
        "statsFieldTwo": "kills"
      }
    ]
  },
  {
    "metadata": [
      {
        "key": '1',
        "name": "abby"
      }
    ],
    "stats": [
      {
        "statsFieldOne": "wins"
      },
      {
        "statsFieldTwo": "kills"
      }
    ]
  }
]

Desired result

     {
       "metadata": [
          {
            "key": '1',
            "name": "sparky"
          }
        ],
        "stats": [
          {
            "statsFieldOne": "wins"
          },
          {
            "statsFieldTwo": "kills"
          }
        ]
      }

2 Answers 2

2

I guess you can do following:

function getObjectForName(key, name) {
    var filteredMetadata = [];
    for(var i=0; i< objectArray.length; i++) {
        filteredMetadata = objectArray[i].metadata.filter((val) => val[key] === name)
        if(filteredMetadata.length) {
            return objectArray[i];
        }   
    }
}
getObjectForName('name', 'sparky')

What this code basically does is, iterates through all objects and check if name is sparky, if yes just break it. If you want to return all occurrences matching name, you need to add all of them to another array and return it.

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

1 Comment

Thanks kanhai, that worked perfectly and I can reuse the function to pull out other objects for specific values. Thanks for your help!
0

You can simply use Reduce

let objectArray = [{"metadata":[{"key":'1',"name":"sparky"}],"stats":[{"statsFieldOne":"wins"},{"statsFieldTwo":"kills"}]},{"metadata":[{"key":'1',"name":"abby"}],"stats":[{"statsFieldOne":"wins"},{"statsFieldTwo":"kills"}]}]

let op = objectArray.reduce(( op,{metadata,stats} ) =>{
  let found = metadata.find(({name})=>name==='sparky')
  if(found){
    op.push({metadata:found,stats})
  } 
  return op
},[])

console.log(op)

2 Comments

Thanks for your help! The issue is that the object with the name sparky won't always be in the [0] position in the array. It could change since it's data I'm pulling from an external API that I don't have control over. How would I use a filter in that case?
@bzlight you can check the update. i hope it will help.

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.