0
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?

5
  • please add valid data. Commented Oct 25, 2019 at 20:02
  • What value do you want from which array? Commented Oct 25, 2019 at 20:10
  • Sorry I have tried it to many ways and copied over to this system that has internet and your right was not right. Commented Oct 25, 2019 at 20:32
  • var array [ machines:{ node: "01", disks:[{ sdasize: '20', sdbsize: '200', }], ram: "8" }, machines:{ node: "01", disks:[{ sdasize: '20', sdbsize: '200', }], ram: "8" }, ] Commented Oct 25, 2019 at 20:33
  • I want to be able to find any of the values Commented Oct 25, 2019 at 20:35

2 Answers 2

1

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);

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

6 Comments

great . . . :-)
What if is was different like the one below
it is simply not valid.
Sorry I copied it twice let me fix it
Is it the machines key that is making it not possible or is it my syntax
|
0

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"
    },
];

1 Comment

Would this also be the same?

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.