我有以下对象:
[ 13,
{ a: [
[ '2988.30000', '0.19000000', '1549294216.653040' ]
]
},
{ b: [
[ '2988.30000', '0.00000000', '1549294216.653774' ],
[ '2985.30000', '0.20000000', '1549294216.558703' ],
[ '2982.00000', '0.08000000', '1549294195.246025' ],
[ '2956.00000', '0.07686000', '1549287593.202601' ],
[ '2802.00000', '0.93779146', '1549187562.171529' ],
[ '1702.50000', '0.05873000', '1548923730.844040' ]
]
}
]
如何检查具有数组'b'的元素是否存在?
编辑 “ b”数组可能位于“ a”之前,“ 13”之前,甚至可能不存在。
答案 0 :(得分:2)
您可以对要检查的条件使用find()。如果某些元素满足条件,则返回它,否则返回undefined
。
const arr = [
13,
{a: [
['2988.30000', '0.19000000', '1549294216.653040']
]},
{b: [
['2988.30000', '0.00000000', '1549294216.653774'],
['2985.30000', '0.20000000', '1549294216.558703'],
['2982.00000', '0.08000000', '1549294195.246025'],
['2956.00000', '0.07686000', '1549287593.202601'],
['2802.00000', '0.93779146', '1549187562.171529'],
['1702.50000', '0.05873000', '1548923730.844040']
]}
];
let res = arr.find(
x => x.hasOwnProperty("b") && Array.isArray(x.b)
);
console.log(res);
答案 1 :(得分:2)
因此它来自Kraken API,并且数据确实具有保证的形状,其中as
和bs
是已知的键->
[ Integer // channel
, { as: [ [ Float, Float, Float ] ] // [ price, volume, timestamp ]
, { bs: [ [ Float, Float, Float ] ] // [ price, volume, timestamp ]
]
XY problem的结果(包括在上面的注释中我的建议)循环遍历对象并动态搜索属性。在这种情况下,我将使用解构分配-
const messageBook =
[ 0
, { as:
[ [ 5541.30000, 2.50700000, 1534614248.123678 ]
, [ 5541.80000, 0.33000000, 1534614098.345543 ]
, [ 5542.70000, 0.64700000, 1534614244.654432 ]
]
}
, { bs:
[ [ 5541.20000, 1.52900000, 1534614248.765567 ]
, [ 5539.90000, 0.30000000, 1534614241.769870 ]
, [ 5539.50000, 5.00000000, 1534613831.243486 ]
]
}
]
// ! ! !
const doSomething = ([ channel, { as }, { bs } ]) =>
console.log(channel, as, bs)
// 0 [...] [...]
doSomething(messageBook)
如果您不能信任生产者,则可以将解构赋值与默认参数结合使用–
const badMessageBook =
[ 0
, { as:
[ [ 5541.30000, 2.50700000, 1534614248.123678 ]
, [ 5541.80000, 0.33000000, 1534614098.345543 ]
, [ 5542.70000, 0.64700000, 1534614244.654432 ]
]
}
// missing { bs: [ ... ] }
]
// ! ! !
const doSomething = ([ channel = 0, { as } = { as: [] }, { bs } = { bs: [] } ]) =>
console.log(channel, as, bs)
// 0 [...] [...]
doSomething(badMessageBook)
在这种情况下,您的函数会有点长。您可以使用多行来增加可读性-
const doSomething = // helpful whitespace emerges...
( [ channel = 0 // default channel 0
, { as } = { as: [] } // sometimes excluded by Kraken
, { bs } = { bs: [] } // sometimes excluded by Kraken
]
) => console.log(channel, as, bs) // doSomething
答案 2 :(得分:0)
const arr = [13,
{
a: [
['2988.30000', '0.19000000', '1549294216.653040']
]
},
{
b: [
['2988.30000', '0.00000000', '1549294216.653774'],
['2985.30000', '0.20000000', '1549294216.558703'],
['2982.00000', '0.08000000', '1549294195.246025'],
['2956.00000', '0.07686000', '1549287593.202601'],
['2802.00000', '0.93779146', '1549187562.171529'],
['1702.50000', '0.05873000', '1548923730.844040']
]
}
];
arr.forEach(el => {
if (el.hasOwnProperty('b')) {
console.log(Array.isArray(Object.values(el)));
}
});
//solution 2 :
let res2 = arr.findIndex(e => e && e['b'] && Array.isArray(e['b'])) > -1;
console.log(res2);
答案 3 :(得分:0)
let list = [ 13,
{ a: [ [ '2988.30000', '0.19000000', '1549294216.653040' ] ] },
{ b:
[ [ '2988.30000', '0.00000000', '1549294216.653774' ],
[ '2985.30000', '0.20000000', '1549294216.558703' ],
[ '2982.00000', '0.08000000', '1549294195.246025' ],
[ '2956.00000', '0.07686000', '1549287593.202601' ],
[ '2802.00000', '0.93779146', '1549187562.171529' ],
[ '1702.50000', '0.05873000', '1548923730.844040' ] ] } ];
if (arrayContainsObject('b', list)) {
console.log('list key b is an Array');
}
function arrayContainsObject(obj, list) {
for (var i = 0; i < list.length; i++) {
if ((typeof list[i] === 'object') && (list[i].hasOwnProperty(obj))) {
return true;
}
}
return false;
}