Add id to array of objects - Javascript

后端 未结 8 1757
星月不相逢
星月不相逢 2021-02-15 10:27

I have an array of objects. How do I add an id key to them starting from 1.

[
{
    color: \"red\",
    value: \"#f00\"
},
{
    color: \"green\",
    value: \"         


        
相关标签:
8条回答
  • 2021-02-15 11:05

    You can use .forEach() to iterate over array elements and add id:

    data.forEach((o, i) => o.id = i + 1);
    

    Demo:

    let data = [{
        color: "red",
        value: "#f00"
    }, {
        color: "green",
        value: "#0f0"
    }, {
        color: "blue",
        value: "#00f"
    }, {
        color: "cyan",
        value: "#0ff"
    }, {
        color: "magenta",
        value: "#f0f"
    }, {
        color: "yellow",
        value: "#ff0"
    }, {
        color: "black",
        value: "#000"
    }];
    
    data.forEach((o, i) => o.id = i + 1);
    
    console.log(data);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    0 讨论(0)
  • 2021-02-15 11:07

    You can use the map() function to iterate over your array of objects.

    n is each of your objects and you can set the id value inside the map.

    Hope this helps :)

    let arr = [{
        color: "red",
        value: "#f00"
      },
      {
        color: "green",
        value: "#0f0"
      },
      {
        color: "blue",
        value: "#00f"
      },
      {
        color: "cyan",
        value: "#0ff"
      },
      {
        color: "magenta",
        value: "#f0f"
      },
      {
        color: "yellow",
        value: "#ff0"
      },
      {
        color: "black",
        value: "#000"
      }
    ]
    
    let i = 0;
    arr.map(n => {
      n['id'] = i;
      i++;
    })
    
      console.log(arr);

    0 讨论(0)
  • 2021-02-15 11:17

    The simples solution would be to use map function

    let data = [
    {
        color: "red",
        value: "#f00"
    },
    {
        color: "green",
        value: "#0f0"
    },
    {
        color: "blue",
        value: "#00f"
    },
    {
        color: "cyan",
        value: "#0ff"
    },
    {
        color: "magenta",
        value: "#f0f"
    },
    {
        color: "yellow",
        value: "#ff0"
    },
    {
        color: "black",
        value: "#000"
    }
    ]
    
    data = data.map((x, i) => {
      x.id = i + 1
      return x
    })
    
    console.log(data)

    0 讨论(0)
  • 2021-02-15 11:19

    when i use this solutions my ids get one number like this:

    list = [ {
        color: "red",
        value: "#f00",
        id: 0
    }, {
    
        color: "green",
        value: "#0f0",
        id: 4
    },{
        color: "blue",
        value: "#00f",
        id: 4
    },{
        color: "cyan",
        value: "#0ff",
        id: 4
    },{
        color: "black",
        value: "#000",
        id: 4
    }] 
    

    my code is this:

    let i = 0;
    list.map(n => {
            n['id'] = i;
            i++;
    });
    
    0 讨论(0)
  • 2021-02-15 11:20

    You could use Array#forEach for this. The second argument of the callback refers to the index of the element. So you can assign the ID as index + 1.

    const source = [{
        color: "red",
        value: "#f00"
      },
      {
        color: "green",
        value: "#0f0"
      },
      {
        color: "blue",
        value: "#00f"
      },
      {
        color: "cyan",
        value: "#0ff"
      },
      {
        color: "magenta",
        value: "#f0f"
      },
      {
        color: "yellow",
        value: "#ff0"
      },
      {
        color: "black",
        value: "#000"
      }
    ];
    
    source.forEach((item, i) => {
      item.id = i + 1;
    });
    
    console.log(source);

    0 讨论(0)
  • 2021-02-15 11:20

    No need for complex function and logic. Simply loop it over forEach which will also give you the index of each object in the array and assign that index+1 value to the id property of the object.

    var arr = [
    {
        color: "red",
        value: "#f00"
    },
    {
        color: "green",
        value: "#0f0"
    },
    {
        color: "blue",
        value: "#00f"
    },
    {
        color: "cyan",
        value: "#0ff"
    },
    {
        color: "magenta",
        value: "#f0f"
    },
    {
        color: "yellow",
        value: "#ff0"
    },
    {
        color: "black",
        value: "#000"
    }
    ];
    
    arr.forEach((item, index)=>{
     item.id = index+1;
    });
    console.log(arr);

    0 讨论(0)
提交回复
热议问题