我是JavaScript的新手,所以我的数组映射技巧不好,我怎么能在这个数组中找到47243781293的assetid?谢谢。
EconItem {
appid: 440,
contextid: '2',
assetid: '4723781293',
classid: '2674',
instanceid: '11040547',
amount: 1,
missing: false,
currency: false,
background_color: '3C352E',
icon_url: '...',
icon_url_large: '...',
tradable: false,
actions:
[ { link: 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=5002&lang=en_US',
name: 'Item Wiki Page...' } ],
name: 'Refined Metal',
name_color: '7D6D00',
type: 'Level 3 Craft Item',
market_name: 'Refined Metal',
market_hash_name: 'Refined Metal',
commodity: false,
market_tradable_restriction: 7,
market_marketable_restriction: 0,
id: '4723781293',
fraudwarnings: [],
descriptions: [],
owner_descriptions: [],
owner_actions: [],
tags: [],
marketable: false
}
答案 0 :(得分:0)
我认为你的意思是在这个JSON"中询问" ..这是47243781293。而不是" ...这个阵列中的47243781293"。您粘贴的对象是JSON表示。如果这就是您的意思,请阅读以下内容 -
鉴于密钥id将始终存在并且上面粘贴的对象值被分配给变量EconItem,我会尝试这样的事情
If( EconItem['id'] === '47243781293' )
{
console.log('Asset id: 47243781293 present in the JSON object');
}
如果您不确定密钥ID将始终存在于对象中,我将首先使用Object.keys()检查密钥是否存在。详细信息可在此处找到 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
希望有所帮助!
答案 1 :(得分:0)
要从对象获取值,请参阅它的关键字:EconItem.assetid
对于具有多个结果的对象,您可以对其进行迭代,并从对象输出特定值,如下所示:
for (var i in EconItem)
{
console.log(EconItem[i].assetid);
//do more here
}
答案 2 :(得分:0)
要在数组中找到它,您可以使用filter
这是每个javascript数组附带的函数,使用上面的对象为每个EconItem
let array = [EconItem, EconItem]
search = array.filter(eachItem=>eachItem.assetId==='47243781293');
--> returns an array of items with assetId as 47243781293, now the first item should be your EconItem, i.e search[0];
检查示例代码段
var items = [{
appid: 440,
contextid: '2',
assetid: '4723781293'
}, {
appid: 441,
contextid: '2',
assetid: '4723781292'
}];
// now we search with this 4723781293
var search = items.filter(function(item){
return item.assetid === '4723781293';
});
//show our search result
alert ("item appid is:"+search[0].appid+", context:"+search[0].contextid+", assetid:"+search[0].assetid);