'if i had an array in which i had 2 objects both had same key name and only want to print the value of one key

I was Learning JavaScript and Stuck on this problem that if i had an array in which i had 2 objects like this -

const datas = [{name: "Father", age: 39}, {name: "Mother", age: 40}]
for (let data of datas){
    console.log(data.age)
}

and the output when i print age - 39 40

How can i only print one value like only console.log() the value mother's age only

I tried To find it on google but no one could help if you could help it would mean a lot to me



Solution 1:[1]

Just add a condition, like that:

const datas = [{name: "Father", age: 39}, {name: "Mother", age: 40}]

for (let data of datas) {
    if (data.name === "Mother") {
        console.log(data.age)
    }
}

or you can search through an array, for the necessary object, and then print its age property:

const mother = datas.find(obj => obj.name === "Mother");

console.log(mother.age);

Good luck!

Solution 2:[2]

First you should filter out the values, what you would like to get. Use filter method.

const datas = [{name: "Father", age: 39}, {name: "Mother", age: 40}]

datas.filter(data=>data.name === "Mother").forEach(data=>console.log(data));

Solution 3:[3]

Welcome to the JavaScript.

for (let data of datas)

This is actually called for..of loop in javascript. This is responsible for iterating through an array. So you will get every object by using the data variable.

Now as the for..of loop iterates through every element of an array so, if you need to take only one possible value from it, then you have to use if condition here.

In your case-

for (let data of datas) {
  if (data.name === 'Mother') {
    console.log(data.age);
  }
}

Note that, if you only need the mother's age then you could also use the break keyword for getting out of the loop.

for (let data of datas) {
  if (data.name === 'Mother') {
    console.log(data.age);
    break;
//--^^^^^^--------
  }
}

Solution 4:[4]

const data = [{name: "Father", age: 39}, {name: "Mother", age: 40}]

let res = data.find(item => item.name === 'Mother')
console.log(res)

Solution 5:[5]

Option A: What you have to do is if you don't know the position of Mother, ask for each name key in the array until it matches, and then if you get a match, just capture the value and print it:

const datas = [{name: "Father", age: 39}, {name: "Mother", age: 40}]
var mothersAge=0;
for (let data of datas){
  if(data.name==="Mother") mothersAge=data.age;
}
console.log(mothersAge);

Option B: You can do all that just with one code line, using ".find()" wich will find you the first element that meets the condition you put:

var mom=datas.find(elem => elem.name==="Mother");
console.log(mom.age);

Or:

console.log(datas.find(elem => elem.name==="Mother").age);

And another way to do it is using filter that returns an array with all the matches to the condition that you put on it. You use .pop() to get the last matching item or .shift() to get the first one(These two options remove the elements of the resulting array to the filter):

var mom=datas.filter(elem => elem.name==="Mother").shift();
console.log(mom.age);

Here they explain it to you with examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 true_k
Solution 2 exphoenee
Solution 3 Sajeeb Ahamed
Solution 4 p4avinash
Solution 5 Weily Suarez Miranda