gpt4 book ai didi

javascript - 确定 JSON 数组中字符串的出现次数

转载 作者:行者123 更新时间:2023-12-02 18:07:44 25 4
gpt4 key购买 nike

我有一个字符串化数组:

JSON.stringify(arr) = [{"x":9.308,"y":6.576,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25},{"x":9.42,"y":7.488,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25}]

我需要找出黄色这个词出现了多少次,这样我就可以执行以下操作:

numYellow = 0;
for(var i=0;i<arr.length;i++){
if(arr[i] === "yellow")
numYellow++;
}

doSomething = function() {
If (numYellow < 100) {
//do something
}
If(numYellow > 100) {
//do something else
} else { do yet another thing}
}

最佳答案

数组的每个元素都是一个对象。将 arr[i] 更改为 arr[i].color。不过,这确实假设 .color 属性是唯一存在 yellow 的位置。

关于javascript - 确定 JSON 数组中字符串的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19957667/

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