admin管理员组

文章数量:1415460

i have this array of Object :

myArray = [
        {
            "edoId": "4010",
            "storeName": "ABBEVILLE"
        },
        {
            "edoId": "3650",
            "storeName": "AGEN"
        },
        {
            "edoId": "3298",
            "storeName": "AIX ALLEES PROVENCALES"
        },
        {
            "edoId": "3309",
            "storeName": "AIX JAS DE BOUFFAN"
        },
        {
            "edoId": "3313",
            "storeName": "AIX LA PIOLINE"
        },
        {
            "edoId": "2119",
            "storeName": "AIX LES BAINS"
        },
    ...
    ]

i want to an auto-incremntal id property for each object of my array , using forEach ( not looping with myArray.length )

Suggestions ?

i have this array of Object :

myArray = [
        {
            "edoId": "4010",
            "storeName": "ABBEVILLE"
        },
        {
            "edoId": "3650",
            "storeName": "AGEN"
        },
        {
            "edoId": "3298",
            "storeName": "AIX ALLEES PROVENCALES"
        },
        {
            "edoId": "3309",
            "storeName": "AIX JAS DE BOUFFAN"
        },
        {
            "edoId": "3313",
            "storeName": "AIX LA PIOLINE"
        },
        {
            "edoId": "2119",
            "storeName": "AIX LES BAINS"
        },
    ...
    ]

i want to an auto-incremntal id property for each object of my array , using forEach ( not looping with myArray.length )

Suggestions ?

Share Improve this question asked Sep 13, 2018 at 16:46 firasKoubaafirasKoubaa 6,88530 gold badges87 silver badges164 bronze badges 3
  • i want sounds like you want us to do the work for you, instead of helping because you're stuck. What have you tried, what doesn't work? – VLAZ Commented Sep 13, 2018 at 16:48
  • so whats the problem? try myArray = myArray.map((i, idx) => ({...i, id: idx})) – givehug Commented Sep 13, 2018 at 16:50
  • when iterate array.forEach((item,index)) , you will get item and index , the index you can use it as an id. – Jameel Moideen Commented Sep 13, 2018 at 16:50
Add a ment  | 

2 Answers 2

Reset to default 4

You can use forEach() method as you want in your question:

myArray.forEach(function(item, index){
    item.id = index;
    // or do whatever you want using index
});

forEach() executes the provided callback once for each element present in the array in ascending order. It is not invoked for index properties that have been deleted or are uninitialized (i.e. on sparse arrays). callback is invoked with three arguments:

  1. the element value
  2. the element index
  3. the array being traversed

var myArray = [
        {
            "edoId": "4010",
            "storeName": "ABBEVILLE"
        },
        {
            "edoId": "3650",
            "storeName": "AGEN"
        },
        {
            "edoId": "3298",
            "storeName": "AIX ALLEES PROVENCALES"
        },
        {
            "edoId": "3309",
            "storeName": "AIX JAS DE BOUFFAN"
        },
        {
            "edoId": "3313",
            "storeName": "AIX LA PIOLINE"
        },
        {
            "edoId": "2119",
            "storeName": "AIX LES BAINS"
        }
    ];

myArray.forEach(function(item, index){
  item.id = index;
});

console.log(myArray);

This should do the trick

myArray.map(i => ({edoId: ++i.edoId ,storeName: i.storeName }))

本文标签: javascriptTypescriptAdd an autoincrement id attribute of each object in arrayStack Overflow