从Node.js中的Array对象中删除特定属性 [英] Remove specific properties from Array objects in Node.js

查看:39
本文介绍了从Node.js中的Array对象中删除特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这个数组,如果我将其字符串化,它将像这样:

For example I have this array, if I stringfy it it would be like this:

[{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}]

我该如何从2辆车上拆除:车门和价格.并且只保留数组"car"和"id"吗?例如:

How can I do for remove from the 2 cars: the doors and price. And only leave in the array "car" and "id"? For example:

[{"car":"Toyota","ID":"1"},{"car":"Chevrolet","ID":"2"}]

谢谢!

推荐答案

您可以使用

map()方法使用调用a的结果创建一个新数组在调用数组中的每个元素上提供了功能.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

这应该是您的代码:

var results = arr.map(function(item){
  return {car : item["car"], ID : item["ID"]}
});

演示:

var arr = [{"car":"Toyota","ID":"1", "Doors": "4", "price": "20.000"},{"car":"Chevrolet","ID":"2", "Doors": "2", "price": "15.000"}];

var results = arr.map(function(item){
  return {car : item["car"], ID : item["ID"]}
});
console.log(JSON.stringify(results));

这篇关于从Node.js中的Array对象中删除特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆