如何在JSON
文件中计算出一个名称的出现次数。我想知道多少次,让我们说名字" MICO",出现在JSON
文件中,我想把它存储在像var mico=2;
这样的变量中。我的JSON
文件如下所示:
[
{
"igralec1": "LEON",
"datum": "Cetrtek - 21. Julij 2016",
"set1": "4",
"set2": "6",
"set3": "6",
"set4": "-",
"set5": "-",
"igralec2": "MICO",
"set21": "6",
"set22": "1",
"set23": "4",
"set24": "-",
"set25": "-"
},
{
"igralec1": "MICO",
"datum": "Ponedeljek - 11. Julij 2016",
"set1": "6",
"set2": "6",
"set3": "6",
"set4": "-",
"set5": "-",
"igralec2": "PERO",
"set21": "2",
"set22": "1",
"set23": "3",
"set24": "-",
"set25": "-"
}
]
答案 0 :(得分:1)
假设已经(以某种方式)将JSON检索到名为json
的变量中,您必须解析它以获取数组:
var array = JSON.parse(json);
虽然如果您使用jQuery Ajax方法来获取它,jQuery可能会为您解析它,那么您可以跳过该步骤。无论如何,假设解析数据在({1}}变量中是(某种方式):
array
演示:https://jsfiddle.net/rzmada6p/1/
我使用了两个function searchForWord(word, array) {
return array.reduce(function(s, o) {
if (o.igralec1 === word) s++;
if (o.igralec2 === word) s++;
return s;
}, 0);
}
var micoCount = searchForWord("MICO", array); // 2
var peroCount = searchForWord("PERO", array); // 1
语句,以防同一对象中的if
和igralec1
属性都包含您要查找的字词。如果您知道它只会是任何给定对象中的一个或另一个属性,那么您可以使用一个igralec2
和一个或:
if
进一步阅读:the array .reduce()
method。
答案 1 :(得分:0)
使用RegExp
构造函数和String.match
函数的解决方案:
var jsonData= '[{"igralec1": "LEON","datum":"Cetrtek - 21. Julij 2016","set1": "4","set2": "6","set3": "6","set4": "-","set5": "-","igralec2": "MICO","set21": "6","set22": "1","set23": "4","set24": "-","set25": "-"},{"igralec1": "MICO","datum": "Ponedeljek - 11. Julij 2016","set1": "6","set2": "6","set3": "6","set4": "-","set5": "-","igralec2": "PERO","set21": "2","set22": "1","set23": "3","set24": "-","set25": "-"}];';
var getCount = function(jsonData, str){
var re = new RegExp('\"' + str +'\"', "ig");
return jsonData.match(re).length;
};
console.log(getCount(jsonData, "MICO")); // 2
console.log(getCount(jsonData, "LEON")); // 1
答案 2 :(得分:0)
function count(data, entry) {
return data
.map(obj =>
Object
.keys(obj)
.map(key => obj[key])
.filter(item => item === entry)
.length)
.reduce((res, val) => res + val, 0);
}
用法:
const data = []; // your array here
console.log(count(data, 'MICO')); // 2
如果你知道钥匙:
function count(data, keys, entry) {
return data1
.reduce((res, obj) =>
keys.forEach(key => obj[key] === entry && res++) || res, 0);
}
用法:
const data = []; // your array here
console.log(count(data, ['igralec1', 'igralec2'], 'MICO')); // 2
注意它使用es2015。
答案 3 :(得分:0)
您可以使用Jquery grep函数。 示例代码将如下所示。 我假设您的JSON数组名称是arr Working code
var array = [
{
"igralec1": "LEON",
"datum": "Cetrtek - 21. Julij 2016",
"set1": "4",
"set2": "6",
"set3": "6",
"set4": "-",
"set5": "-",
"igralec2": "MICO",
"set21": "6",
"set22": "1",
"set23": "4",
"set24": "-",
"set25": "-"
},
{
"igralec1": "MICO",
"datum": "Ponedeljek - 11. Julij 2016",
"set1": "6",
"set2": "6",
"set3": "6",
"set4": "-",
"set5": "-",
"igralec2": "PERO",
"set21": "2",
"set22": "1",
"set23": "3",
"set24": "-",
"set25": "-"
}
];
var MICO=0;
var objJson = jQuery.grep(array, function( n, i ) {
if(n.igralec1=="MICO" || n.igralec2 =="MICO"){
MICO++;
}
});
alert(MICO);