Reputation: 33
Im using Angular 6, I have list and Im exporting it to CSV file using Angular5-csv, I want to remove the last column of the list i.e., the last element of each array in the list. my list looks like
let traceslist = [
{
"name": "abcd",
"email": "[email protected]",
"decision": "yes",
"userid": "abcd"
},
{
"name": "phill";
"email": "[email protected]";
"decision": "yes";
"userid": "phill";
},
{
"name": "raj";
"email": "[email protected]";
"decision": "no";
"userid": "raj";
},
{
"name": "john";
"email": "[email protected]";
"decision": "yes";
"userid": "john";
}
]
now, I want to remove the elements userid, so that the column will not be present in my csv file, from the list. I have tried using splice but that was not successful.
It'd be great if anyone of you can help me with this.
Upvotes: 1
Views: 10684
Reputation: 182
You can't use delete to remove an item from an array. This is only used to remove a property from an object.
You should use splice to remove an element from an array:
deleteMsg(removeElement:string) {
const index: number = traceslist.indexOf(removeElement);
if (index !== -1) {
traceslist.splice(index, 1);
}
}
Upvotes: 2
Reputation: 3502
First of all, your JSON format is wrong, semicolon (;) is first and the strings should be in quotes check it below
let obj = [{
name: "abcd",
email: "[email protected]",
decision: "yes",
userid: "abcd",
},
{
name: "abcd",
email: "[email protected]",
decision: "yes",
userid: "abcd",
},
{
name: "raj",
email: "[email protected]",
decision: "no",
userid: "raj",
},
{
name: "john",
email: "[email protected]",
decision: "yes",
userid: "john",
}
]
let filtered = obj.map(item => {
delete item.userid;
return item;
});
console.log(filtered);
Upvotes: 0
Reputation: 362
We can use .map() method to achieve this
let traceslist = [
{
name: 'abcd',
email: '[email protected]',
decision: 'yes',
userid: 'abcd',
},
{
name: 'abcd',
email: '[email protected]',
decision: 'yes',
userid: 'abcd',
},
{
name: 'abcd',
email: '[email protected]',
decision: 'yes',
userid: 'abcd',
},
{
name: 'abcd',
email: '[email protected]',
decision: 'yes',
userid: 'abcd',
}
]
const result = traceslist.map(o => {
delete o.userid;
return o;
})
console.log(result);
Upvotes: 0
Reputation: 918
This is a simple javascript problem. Also you need to convert your data to string. See the example below:
let traceslist = [
{
'name': 'abcd',
'email': '[email protected]',
'decision': 'yes',
'userid': 'abcd'
},
{
'name': 'abcd',
'email': '[email protected]',
'decision': 'yes',
'userid': 'abcd'
}
];
traceslist.forEach( item => delete item.userid );
console.log(traceslist);
Upvotes: 0
Reputation: 573
Use the .map
array method to remove the userid from every item in your array.
traceslist = traceslist.map(item => {
delete item.userid;
return item;
});
By the way, it's not angular related so your title and tag is a little misleading. An array is the same in pure javascript.
Upvotes: 2