您好,我正在尝试从数组数据中的每个对象中删除操作属性:
[{productName: "", ... action: ...,}, {productName: "", ...action:...} ...]
var arr =[{productName: "bbbb", action: 'b'},
{productName: "aa",action: 'a'},
{productName: "tt",action: 't'},
{productName: "vv",action: 'v'}]
arr = arr.map((d) => {delete d.action ; return d;});
1) With map(...)
:
your_array.map(o => {'productName': o.productName})
NB that this is more elegant if you want to filter off lots of attributes, but more work if you just want to remove one.
2) With delete
:
for (let i = 0; i < your_array.length; i++){
delete your_array[i].action
}
Iterate over the array using map
and destructure the object to keep only the required field
var arr = [{ producet: 'xyz', action: 'abc', other: 'o' }, { producet: 'xyz', action: '123', other: 'o' }, { producet: 'xyz', action: 'sdf', other: 'o' }] const result = arr.map(({ action, ...rest }) => rest); console.log(result);
With ES6 you can use map
with destructuring and rest parameters to separate out the object properties you want to retain, and the part you want to discard:
const data = [{ id: 1, productName: "1", action: '' }, { id: 1, productName: "2", action: '' }]; let out = data.map(({ action, ...rest }) => rest); console.log(out);
Use array map
It will return a new array.Inside map
callback create a new object and populate only those fields that are required. Don't mutate the original array
var arr = [{ productName: "1", action: '' }, { productName: "2", action: '' }]; let newArr = arr.map(function(item) { return Object.assign({}, { productName: item.productName }) }); console.log(newArr)
According to the docs you can use the delete
operator.
A simple way to do this is to loop over your array arr
and delete the property from each object.
for (var i = 0; i < arr.length; i++){
delete arr[i].action;
}
This approach is slightly faster than other approaches such as map
and for each
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.