最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to remove an element from a list typescript - Stack Overflow

programmeradmin3浏览0评论

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

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.

Share Improve this question edited Nov 30, 2018 at 13:36 Nithin Raj Teddu asked Nov 30, 2018 at 13:23 Nithin Raj TedduNithin Raj Teddu 331 gold badge1 silver badge4 bronze badges 2
  • 1 The values in your (key,value) pairs should be in string form, and should end with a a, not a semi colon. – Benni Russell Commented Nov 30, 2018 at 13:28
  • Sorry, it was a firebase list which i could not copy paste here, so i made a sample here and missed adding quotes, that was my mistake sorry and Thanks for your time. – Nithin Raj Teddu Commented Nov 30, 2018 at 13:54
Add a ment  | 

5 Answers 5

Reset to default 2

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.

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论