-2

This is my code. I have an array of object with 5 fields - name, achievements, points, age, city

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'];

I want to remove this two fields. I cannot use jQuery or lodash. I want to do this using Plain JS. How should I do this? Here I have 5 attributes and I want to remove 2 but in reality I will have more attributes. So I need to achieve this with the help of loops.

I want to remove removeThisFields from inputData.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
tommy123
  • 587
  • 1
  • 10
  • 28
  • 1
    *remove* means mutating the original or do you want to get a new array/objects? btw, what have you tried? – Nina Scholz Feb 07 '19 at 14:35
  • "*I want*" isn't a question; what have you tried? Where did you fail? What exact end-result do you want? What help do you need? – David Thomas Feb 07 '19 at 14:40
  • I think it would be easy for you to understand and remove, there is alot of method for delete like map, filter... for( let i = 0; i < inputData.length; i++ ) { delete inputData[i].name; delete inputData[i].age; } delete is build-in function for deleting something in obj Cheer you! – Ericgit Feb 07 '19 at 14:46

5 Answers5

0

Looks interesting. Here is an idea of would be.

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);
Álvaro Touzón
  • 1,247
  • 1
  • 8
  • 21
  • Please don't pass the whole array instead pass the var removeThisFields. Now I want to remove 2 fields, in future i may need to remove 3 or n number of attributes or fields – tommy123 Feb 07 '19 at 14:39
0

You can create your own omit function using reduce method and then pass it to map method.

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)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

You can use the delete operator to remove keys from an object. It should look like this

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);
molamk
  • 4,076
  • 1
  • 13
  • 22
0

You could filter the keys and build new objects.

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; }

Or mutate the objects.

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; }

Mutating with 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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Try this:

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

console.log(JSON.stringify(results));
Bodrov
  • 840
  • 1
  • 15
  • 29
  • Why are you targeting the specific fields. If i have larger number of fields to remove, it becomes very difficult – tommy123 Feb 07 '19 at 15:29