按属性值从对象数组中获取JavaScript对象假设我有一个由四个对象组成的数组:var jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4},
{a: 5, b: 6},
{a: 7, b: 8}];有什么方法可以获得第三个对象({a: 5, b: 6})按财产的价值计算b例如,如果没有for...in循环?
3 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
Filter
var result = jsObjects.filter(obj => { return obj.b === 6})
const jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4},
{a: 5, b: 6},
{a: 7, b: 8}
]
let result = jsObjects.filter(obj => {
return obj.b === 6
})
console.log(result)
Find
undefined
var result = jsObjects.find(obj => { return obj.b === 6})
const jsObjects = [
{a: 1, b: 2},
{a: 3, b: 4},
{a: 5, b: 6},
{a: 7, b: 8}
]
let result = jsObjects.find(obj => {
return obj.b === 6
})
console.log(result)
添加回答
举报
0/150
提交
取消