简体   繁体   English

从 javascript 多维数组中的对象中删除元素

[英]Remove element from objects in javascript multidimensional array

I have read several solutions to this problem here.我在这里阅读了这个问题的几个解决方案。 When I try it, I continue to receive an error for the pop() method.当我尝试它时,我继续收到 pop() 方法的错误。

I have what is essentially a multidimensional array in javascript.我在 javascript 中有一个多维数组。 I am tasked with returning the array with the sensitive info removed (eg remove the SSN, in this example)我的任务是返回删除敏感信息的数组(例如,在本例中删除 SSN)

I thought I could use a foreach loop, and the pop() function to remove the last element of the child arrays, the SSN.我想我可以使用 foreach 循环和 pop() function 来删除子 arrays 的最后一个元素,即 SSN。

testing it using node on the commandline, the stdout is telling me that element.pop() is not a function.在命令行上使用节点对其进行测试,标准输出告诉我 element.pop() 不是 function。 i've tried it with pop(), slice(), filter(), all with no success.我用 pop()、slice()、filter() 试过了,都没有成功。

when running $> node filename.js运行 $> 节点文件名.js 时

H:\Apache2\htdocs\test\filename.js:50 noppi[i] = element.pop(); H:\Apache2\htdocs\test\filename.js:50 noppi[i] = element.pop(); ^ ^

TypeError: element.pop is not a function类型错误:element.pop 不是 function

let recs = [
    {
        ID: 1,
        NAME: 'John',
        EMAIL: 'john@example.com',
        SSN: '123'
    }, {
        ID: 2,
        NAME: 'Sally',
        EMAIL: 'sally@example.com',
        SSN: '456'
    }, {
        ID: 3,
        NAME: 'Angie',
        EMAIL: 'angie@example.com',
        SSN: '789'
    }
];

let i = 0;
let noppi = [];

recs.forEach(element => {
    noppi[i] = element.pop();
    i++;
    
});

console.log(noppi);  

At the risk of sounding redundant, I'll briefly reiterate what the earlier answers have already stated.冒着听起来多余的风险,我将简要重申之前的答案已经说明的内容。

The input data structure isn't a multi-dimensional array输入数据结构不是多维数组[ [... ], [... ] ] , it's an array of objects [ {...}, {...} ] . ,它是一个对象数组[ {...}, {...} ] So you can't use Array methods like .pop() on the objects {...} .所以你不能在对象{...}上使用像.pop()这样的数组方法。

Here's a simple one-liner that uses .forEach() and delete .这是一个使用.forEach()delete的简单单行代码。

recs.forEach(obj => delete obj.SSN)

delete is an operator with one purpose: to remove an object's property like for example SSN: '123-45-6789' . delete是一个具有一个目的的运算符:删除对象的属性,例如SSN: '123-45-6789' Simple and perfect.简单而完美。

Note, .forEach() mutates the array, meaning that it's the original data being changed (see Minja's comment).请注意, .forEach()会改变数组,这意味着它是正在更改的原始数据(请参阅 Minja 的评论)。

 let recs = [ { ID: 1, NAME: 'John', EMAIL: 'john@example.com', SSN: '123' }, { ID: 2, NAME: 'Sally', EMAIL: 'sally@example.com', SSN: '456' }, { ID: 3, NAME: 'Angie', EMAIL: 'angie@example.com', SSN: '789' } ]; recs.forEach(obj => delete obj.SSN); console.log(recs)

Try this:尝试这个:

recs.forEach(element => {
    noppi.push = element;
});

You are trying to use pop() on an object not an array您正在尝试在 object 上使用 pop() 而不是数组

As per your need you need to remove SSN from your object, try below code it should work for you.根据您的需要,您需要从 object 中删除 SSN,请尝试以下代码,它应该适合您。

recs.forEach(element => {
 const { SSN, ...rest } = element;
    noppi.push(rest);
});

Here we are removing SSN from object and rest will push in noppi.在这里,我们从 object 中删除 SSN,rest 将推送 noppi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM