B Stark
B Stark

Reputation: 11

In javascript how to find value in a array of objects that has a nested array

var array [{
  machines:[{
    node: "01",
    disksize: "75",
    ram: "8"
    },
    node: "02",
    disksize: "100",
    ram: "16"
    },     
  ]
}]

let obj = objArray.find(obj => obj.disksize=== '100');
console.log(obj);

I tried all types of ways to get any value that I'm looking for in the are with no such luck how would this be done in javascript?

Upvotes: 0

Views: 41

Answers (2)

B Stark
B Stark

Reputation: 11

What if your data is different and looks like?

var array = [
  machines:{
    node: "01",
    disks:[{
      sdasize: '20',
      sdbsize: '200',
    }],
    ram: "8"
    },
    machines:{
      node: "02",
      disks:[{
        sdasize: '75',
        sdbsize: '300',
      }],
      ram: "16"
    },
];

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386512

You have a nested array and only the inner array machines has the wanted object. In this case, you could iterate the outer and return the find of the inner array.

var array = [{ machines: [{ node: "01", disksize: "75", ram: "8" }, { node: "02", disksize: "100", ram: "16" }] }],
    result;

array.some(({ machines }) => result = machines.find(({ disksize }) => disksize === '100'));

console.log(result);

Upvotes: 1

Related Questions