gpt4 book ai didi

javascript - 如何在 JavaScript 或 jQuery 中过滤 JSON 数据?

转载 作者:IT老高 更新时间:2023-10-28 12:45:42 26 4
gpt4 key购买 nike

如何使用 Javascript 或 jQuery 过滤 JSON 数据?

这是我的 JSON 数据:

[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}]

JavaScript:

obj1 = JSON.parse(jsondata);

现在我只想要包含网站的名称和网站数据等于 "yahoo"

最佳答案

这是你应该这样做的:(用于谷歌查找)

$([
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
])
.filter(function (i,n){
return n.website==='google';
});

更好的解决方案:(萨尔曼的)

$.grep( [{"name":"Lenovo Thinkpad 41A4298","website":"google"},{"name":"Lenovo Thinkpad 41A2222","website":"google"}], function( n, i ) {
return n.website==='google';
});

http://jsbin.com/yakubixi/4/edit

关于javascript - 如何在 JavaScript 或 jQuery 中过滤 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com