admin管理员组文章数量:1316980
this is my first array
data= [{
"id":1111,
"date":"2020-08-03T08:00:00+00:00",
"age":23,
"email":"[email protected]",
"address":"phill road",
"salary":1222.00
},
{
"id":222,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road",
"salary":3344
}
]
second array should look like this
dataNew= [1222.00,3344]
I need second array that only contain the salary value . How can I filter the first array, that if the keys name = "salary" , push the value to new array. Searched for so many suggestion, but nothing seems working for me..
this is my first array
data= [{
"id":1111,
"date":"2020-08-03T08:00:00+00:00",
"age":23,
"email":"[email protected]",
"address":"phill road",
"salary":1222.00
},
{
"id":222,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road",
"salary":3344
}
]
second array should look like this
dataNew= [1222.00,3344]
I need second array that only contain the salary value . How can I filter the first array, that if the keys name = "salary" , push the value to new array. Searched for so many suggestion, but nothing seems working for me..
Share Improve this question asked Aug 5, 2020 at 15:39 user3643092user3643092 4361 gold badge8 silver badges21 bronze badges5 Answers
Reset to default 4
data= [{
"id":1111,
"date":"2020-08-03T08:00:00+00:00",
"age":23,
"email":"[email protected]",
"address":"phill road",
"salary":1222.00
},
{
"id":222,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road",
"salary":3344
},
{
"id":223,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road"
}
]
console.log(data.filter(e => e.salary).map(e => e.salary));
This
data = [{
"id": 1111,
"date": "2020-08-03T08:00:00+00:00",
"age": 23,
"email": "[email protected]",
"address": "phill road",
"salary": 1222.00
},
{
"id": 222,
"date": "2020-08-03T08:00:00+00:00",
"age": 24,
"email": "[email protected]",
"address": "phill2222 road",
"salary": 3344
}
]
arr = []
for (i = 0; i < data.length; i++) {
console.log(data[i]['salary'])
arr.push(data[i]['salary']);
}
console.log(arr)
data= [{
"id":1111,
"date":"2020-08-03T08:00:00+00:00",
"age":23,
"email":"[email protected]",
"address":"phill road",
"salary":1222.00
},
{
"id":222,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road",
"salary":3344
}]
data.map(item => item.salary)
You can use .map() on the array
data.map(item => item.salary)
data= [{
"id":1111,
"date":"2020-08-03T08:00:00+00:00",
"age":23,
"email":"[email protected]",
"address":"phill road",
"salary":1222.00
},
{
"id":222,
"date":"2020-08-03T08:00:00+00:00",
"age":24,
"email":"[email protected]",
"address":"phill2222 road",
"salary":3344
}
]
const dataNew=[]
data.map(item=>item.salary?dataNew.push(item.salary):null)
本文标签: How to filter an array by key and push the value to another array using javascriptStack Overflow
版权声明:本文标题:How to filter an array by key and push the value to another array using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741999103a2410646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论