假设我有对象数组,
[{"month":"03-2016","isLate":"N","transactionCount":4,"transactionAmount":8746455},{"month":"05-2016","isLate":"N","transactionCount":5,"transactionAmount":-40004952945.61},{"month":"06-2016","isLate":"N","transactionCount":1,"transactionAmount":100000},{"month":"04-2016","isLate":"N","transactionCount":6,"transactionAmount":-117189.89},{"month":"02-2016","isLate":"N","transactionCount":4,"transactionAmount":-5331000},{"month":"04-2016","isLate":null,"transactionCount":2,"transactionAmount":-4888.89},{"month":"03-2016","isLate":null,"transactionCount":3,"transactionAmount":5000}]
在下划线或javascript中有一个函数,我可以根据某些条件循环该项,删除某些对象,否则修改对象(在对象中添加更多信息)并返回数据。
答案 0 :(得分:4)
您可以使用纯JavaScript:
例如:
var input = [
{
"month": "03-2016",
"isLate": "N",
"transactionCount": 4,
"transactionAmount": 8746455
},
{
"month": "05-2016",
"isLate": "N",
"transactionCount": 5,
"transactionAmount": -40004952945.61
},
{
"month": "06-2016",
"isLate": "N",
"transactionCount": 1,
"transactionAmount": 100000
},
{
"month": "04-2016",
"isLate": "N",
"transactionCount": 6,
"transactionAmount": -117189.89
},
{
"month": "02-2016",
"isLate": "N",
"transactionCount": 4,
"transactionAmount": -5331000
},
{
"month": "04-2016",
"isLate": null,
"transactionCount": 2,
"transactionAmount": -4888.89
},
{
"month": "03-2016",
"isLate": null,
"transactionCount": 3,
"transactionAmount": 5000
}
];
// Helper array to translate month number to month name
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
input = input
// only get the entries which don't have isLate=N:
.filter( o => o.isLate !== 'N' )
// add a property "mm":
.map( o => Object.assign(o, { mm: monthNames[+o.month.substr(0,2)-1] }) );
// Output result
console.log(input);
对于没有ES6支持的JavaScript环境,请将input =
语句替换为:
input = input
// only get the entries which don't have isLate=N:
.filter( function (o) {
return o.isLate !== 'N';
})
// add a property "mm":
.map( function (o) {
o.mm = monthNames[+o.month.substr(0,2)-1];
return o;
});
答案 1 :(得分:2)
是的,使用filter()
var resArray = myArray.filter(x => x.transactionCount != 1);
在这个例子中,我删除了属性为transactionCount === 1
的元素