filter
learn
Thank you, first floor
var data = [
{"id":"1","name":"","age":"25"},
{"id":"2","name":"","age":"24"},
{"id":"3","name":"","age":"27"},
{"id":"4","name":"","age":"24"},
{"id":"5","name":"","age":"22"},
];
new_data = data.filter(function(element, index) {
return element['age'] >= 25;
});
console.log(new_data);
var data = [
{"id":"1","name":"","age":"25"},
{"id":"2","name":"","age":"24"},
{"id":"3","name":"","age":"27"},
{"id":"4","name":"","age":"24"},
{"id":"5","name":"","age":"22"},
];
var data1 = []
data.filter(function(e){
if(e.age > 25){
data1.push(e)
}
})
console.log(data1) //{id: "3", name: "", age: "27"}
var data = [
{ "id": "1", "name": "", "age": "25" },
{ "id": "2", "name": "", "age": "24" },
{ "id": "3", "name": "", "age": "27" },
{ "id": "4", "name": "", "age": "24" },
{ "id": "5", "name": "", "age": "22" },
];
var newData= data.filter(v=>v.age >= 25)
Delete less than or equal to 25, and return a new array
function splice(){
for(let i = 0;i<data.length;iPP){
if(parseInt(data[i].age)<26){
data.splice(i,1);
i--;
}
}
console.log(data);
}
var data = [
{ "id": "1", "name": "", "age": "25" },
{ "id": "2", "name": "", "age": "24" },
{ "id": "3", "name": "", "age": "27" },
{ "id": "4", "name": "", "age": "24" },
{ "id": "5", "name": "", "age": "22" },
];
var newData= [];
var res = data.forEach(function (item, index, input) {
if (parseInt(item["age"]) >= 25){
newData.push(item);
}
})
console.log(newData);
using underscore.js 's filter can solve this problem:
var data = [
{ "id": "1", "name": "", "age": "25" },
{ "id": "2", "name": "", "age": "24" },
{ "id": "3", "name": "", "age": "27" },
{ "id": "4", "name": "", "age": "24" },
{ "id": "5", "name": "", "age": "22" },
];
var newData = _.filter(data, function(d){ return d.age > 25; })