如何获取此JavaScript数组中的特定数字?
我是JavaScript新手,所以我的数组映射技能很差,如何在这个数组中找到assetid,即47243781293?多谢各位如何获取此JavaScript数组中的特定数字?,javascript,arrays,Javascript,Arrays,我是JavaScript新手,所以我的数组映射技能很差,如何在这个数组中找到assetid,即47243781293?多谢各位 EconItem { appid: 440, contextid: '2', assetid: '4723781293', classid: '2674', instanceid: '11040547', amount: 1, missing: false, currency: false, background_color:
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
}
我想你的意思是问“.这个JSON中哪个是47243781293”,而不是“.这个数组中哪个是47243781293”。上面粘贴的对象是JSON表示。如果你想问这个问题,请阅读下面的内容-
考虑到键id将始终存在,并且上面粘贴的对象值被分配给变量EconItem,我将尝试以下方法
If( EconItem['id'] === '47243781293' )
{
console.log('Asset id: 47243781293 present in the JSON object');
}
如果您不确定对象中始终存在密钥id,我将首先使用object.keys()检查密钥是否存在。详情可在此找到-
希望有帮助 要从对象中获取值,只需参考它的键:
EconItem.assetid
对于具有多个结果的对象,可以对其进行迭代,并从对象中输出特定值,如下所示:
for (var i in EconItem)
{
console.log(EconItem[i].assetid);
//do more here
}
要在数组中找到它,可以使用
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项目=[{
附件:440,
contextid:'2',
资产负债表:“4723781293”
}, {
附件:441,
contextid:'2',
资产负债表:“4723781292”
}];
//现在我们用这个4723781293搜索
var search=items.filter(函数(项){
return item.assetid=='4723781293';
});
//显示我们的搜索结果
警报(“项目appid为:“+search[0].appid+”,上下文:“+search[0].contextid+”,assetid:“+search[0].assetid”)代码>如何获取值是什么意思?您想从JSON中获取任何值吗?如何从该数组中获取assetid值?您发布的是一个对象,而不是数组。您只是想从这个对象中提取assetid吗?或者存在一个包含多个类似对象的数组,并且您正在尝试查找的对象是assetid==“47243781293”EconItem.assetid
。顺便说一下,这是一个对象。是的,我正试图在这个对象中找到你现在看到的assetid值。