如何从JavaScript中的对象数组中删除选定的属性?

时间:2019-02-07 14:33:53

标签: javascript

这是我的代码。我有一个包含5个字段的对象数组-名称,成就,分数,年龄,城市

var inputData = [{
  name: 'Nick',
  achievements: 158,
  points: 14730,
  age: 23,
  city: 'London'
}, {
  name: 'Jordan',
  achievements: '175',
  points: '16375',
  age: 24,
  city: 'Paris'
}, {
  name: 'Ramon',
  achievements: '55',
  points: '2025',
  age: 25,
  city: 'NYC'
}];

var removeThisFields = ['name', 'age'];

我要删除这两个字段。我不能使用jQuery或lodash。我想使用Plain JS做到这一点。我应该怎么做?在这里,我有5个属性,我想删除2个,但实际上,我将有更多属性。因此,我需要借助循环来实现这一目标。

我想从removeThisFields中删除inputData

5 个答案:

答案 0 :(得分:0)

看起来很有趣。 这是一个将会的想法。

var inputData = [{
name: 'Nick',
achievements: 158,
points: 14730,
age: 23,
city: 'London'
}, {
name: 'Jordan',
achievements: '175',
points: '16375',
age: 24,
city: 'Paris'
}, {
name: 'Ramon',
achievements: '55',
points: '2025',
age: 25,
city: 'NYC'
}];
function removeProps(item, props) {
 props.forEach(prop => {
 	delete item[prop];
 });
 return item;
}

const removed = inputData.map(item => removeProps(item, ['name', 'age']));

console.log(removed);

答案 1 :(得分:0)

您可以使用omit方法创建自己的reduce函数,然后将其传递给map方法。

var data = [{"name":"Nick","achievements":158,"points":14730,"age":23,"city":"London"},{"name":"Jordan","achievements":"175","points":"16375","age":24,"city":"Paris"},{"name":"Ramon","achievements":"55","points":"2025","age":25,"city":"NYC"}]
var arr = ['name', 'age'];

const omit = (obj, arr) => Object.keys(obj).reduce((r, e) => {
  return !arr.includes(e) ? Object.assign(r, {[e]: obj[e]}) : r
}, {})

const res = data.map(o => omit(o, arr));
console.log(res)

答案 2 :(得分:0)

您可以使用delete运算符从对象中删除键。看起来应该像这样

var inputData = [{
  name: 'Nick',
  achievements: 158,
  points: 14730,
  age: 23,
  city: 'London'
}, {
  name: 'Jordan',
  achievements: '175',
  points: '16375',
  age: 24,
  city: 'Paris'
}, {
  name: 'Ramon',
  achievements: '55',
  points: '2025',
  age: 25,
  city: 'NYC'
}];

var removeThisFields = ['name', 'age'];

const filtered = inputData.map(d => {
  removeThisFields.forEach(f => {

    // Delete your key here
    delete d[f];
  });
  return d;
});

console.log(filtered);

答案 3 :(得分:0)

您可以过滤键并构建新对象。

var array = [{ name: 'Nick', achievements: 158, points: 14730, age: 23, city: 'London' }, { name: 'Jordan', achievements: '175', points: '16375', age: 24, city: 'Paris' }, { name: 'Ramon', achievements: '55', points: '2025', age: 25, city: 'NYC' }],
    remove = ['name', 'age'],
    result = array.map(o => Object.assign({}, ...Object
        .keys(o)
        .filter(k => !remove.includes(k))
        .map(k => ({ [k]: o[k] }))
    ));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

或变异对象。

var array = [{ name: 'Nick', achievements: 158, points: 14730, age: 23, city: 'London' }, { name: 'Jordan', achievements: '175', points: '16375', age: 24, city: 'Paris' }, { name: 'Ramon', achievements: '55', points: '2025', age: 25, city: 'NYC' }],
    remove = ['name', 'age'];

array.forEach(o => remove.forEach(k => delete o[k]));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Reflect.deleteProperty

var array = [{ name: 'Nick', achievements: 158, points: 14730, age: 23, city: 'London' }, { name: 'Jordan', achievements: '175', points: '16375', age: 24, city: 'Paris' }, { name: 'Ramon', achievements: '55', points: '2025', age: 25, city: 'NYC' }],
    remove = ['name', 'age'];

array.forEach(o => remove.forEach(Reflect.deleteProperty.bind(null, o)));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:0)

尝试一下:

let results = inputData.map(function(item) {
    return {name: item["name"], age: item["age"]}
});

console.log(JSON.stringify(results));