抱歉,我刚刚更改了这个问题: 我想知道在我的阵列中有多少次我有“橙色”字样。在这个例子中,“橙色”> 4次。
MyArray = [{
fruit1: "Orange is blabla",
fruit2: "Apple blabla",
fruit3: "blabla Banana",
color1: "ORANGE"
}, {
fruit4: "blabla Orange",
fruit5: "Apple",
fruit6: "Banana",
color2: "orange blabla"
}];
var newArr = MyArray.filter(function (item) {
return _.values(item.xxx === "orange";
});
答案 0 :(得分:1)
您可以通过以下方式执行此操作
let MyArray = [{fruit1:"Orange", fruit2:"Apple", fruit3:"Banana", color1:"ORANGE"},{fruit4:"Orange", fruit5:"Apple", fruit6:"Banana", color2:"orange"}];
let result = MyArray.reduce((a, b) => {
for(let key of Object.keys(b)){
let re = /orange/gi;
if(b[key].match(re) != null){
a++;
}
}
return a;
}, 0);
console.log(result);
答案 1 :(得分:0)
MyArray = [{
fruit1: "Orange is blabla",
fruit2: "Apple blabla",
fruit3: "blabla Banana",
color1: "ORANGE"
}, {
fruit4: "blabla Orange",
fruit5: "Apple",
fruit6: "Banana",
color2: "orange blabla"
}];
var totalOrange = 0;
MyArray.map(function(item) {
for (var key in item) {
if (item[key].toLowerCase().indexOf('orange') >= 0) {
totalOrange++;
}
}
});
console.log(totalOrange);
答案 2 :(得分:0)
您可以这样做: -
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
答案 3 :(得分:0)
使用ES6,您可以使用展开式语法...
和Object.values
来执行此操作。
var MyArray = [{ fruit1: "Orange is blabla", fruit2: "Apple blabla", fruit3: "blabla Banana", color1: "ORANGE" }, { fruit4: "blabla Orange", fruit5: "Apple", fruit6: "Banana", color2: "orange blabla" }]
var count = [].concat(...MyArray.map(Object.values))
.filter(e => e.toLowerCase().includes('orange'))
.length;
console.log(count)
答案 4 :(得分:0)
我想这样的事情可能有用。 你可以在一个大循环中做到这一点,使它稍快一些,但这种形式更具可读性。
const amountOfOranges = MyArray
// transform each object into an array of its values
.map( Object.values )
// remove all values not named 'orange'
.map( ary => ary.filter( value => value === 'orange' ) )
// replace all arrays by their length
.map( ary => ary.length )
// sum the results
.reduce( ( x, y ) => x + y, 0 );
答案 5 :(得分:0)
var MyArray = [{fruit1:"Orange", fruit2:"Apple", fruit3:"Banana", color1:"ORANGE"},{fruit4:"Orange", fruit5:"Apple", fruit6:"Banana", color2:"orange"}];
var count = 0;
MyArray.map(function(item,index){
Object.keys(item).map(function(key){
if(item[key].toLowerCase().indexOf('orange') >= 0){
count++;
}
});
});
console.log(count);