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

javascript - Add unique generated Identifier on array of json object - Stack Overflow

programmeradmin1浏览0评论

How to add uniqueId field in below JSON. This array has large number of data and needs to dynamic unique identifier on existing array.

[{"title":"Acpanying"},{"title":"Chamber music"},{"title":"Church 

music"}......]

so, this should look as follow:

[{"title":"Acpanying", "uniqueId": 1},{"title":"Chamber music", "uniqueId": 2}..]

uniqueId- type, number or guid.

Note: don't know the "title" or what other fields could be, so, could not map the fields by name.

How to add uniqueId field in below JSON. This array has large number of data and needs to dynamic unique identifier on existing array.

[{"title":"Acpanying"},{"title":"Chamber music"},{"title":"Church 

music"}......]

so, this should look as follow:

[{"title":"Acpanying", "uniqueId": 1},{"title":"Chamber music", "uniqueId": 2}..]

uniqueId- type, number or guid.

Note: don't know the "title" or what other fields could be, so, could not map the fields by name.

Share Improve this question edited Oct 22, 2018 at 18:53 user3711357 asked Oct 22, 2018 at 18:47 user3711357user3711357 1,6458 gold badges34 silver badges59 bronze badges 1
  • use an accumulator, loop through the array and add in the object the value of the accumulator for each object – Calvin Nunes Commented Oct 22, 2018 at 18:50
Add a ment  | 

3 Answers 3

Reset to default 3

I would go for a simple for loop

let myArray = [{"title":"Acpanying"},{"title":"Chamber music"},{"title":"Church music"}];
let i = 0, ln = myArray.length;
for (i;i<ln;i++){
  myArray[i].uniqueId = i+1;
}

console.log(myArray);

If this is a one time thing you could do the following:

const newArray = oldArray.map((x, i) => ({
    // If the object is dynamic you can spread it out here and add the ID
    ...x,
    // Use the items index in the array as a unique key
    uniqueId: i,
}));

If you want to use a guid generator instead (I'd remend that) just replace i with whatever you use to generate a GUID and ensure that any time you add to the collection you generate a new GUID for the data.

const newArray = oldArray.map((x) => ({ ...x, uniqueId: generateGuid() }));

const yourDynamicObjects = [
    {
        title: 'A title',
        author: 'A. Author'
    },
    {
        foo: 'bar',
    },
    {
        quotient: 2,
        irrational: Math.sqrt(2)
    }
];

const updatedData = yourDynamicObjects.map((x, i) => ({ ...x, uniqueId: i, }));

console.log(updatedData);

You can use map & in it's call back function use the index parameter to create uniqueId

item.title is not known actually as its dynamic array and so, could not map with particular field names

In this case use Object.keys to get an array of all the keys . Then loop over it and add the key to a new object

let k = [{
  "title": "Acpanying"
}, {
  "title": "Chamber music"
}, {
  "title": "Church"
}]
let getArrayKey = Object.keys(k[0]);
let n = k.map(function(item, index) {
  let obj = {};
  getArrayKey.forEach(function(elem) {
    obj[elem] = item[elem];
  })
  obj.uniqueId = index + 1
  return obj;
});

console.log(n)

Also you can use spread operator

let k = [{
  "title": "Acpanying"
}, {
  "title": "Chamber music"
}, {
  "title": "Church"
}]

let n = k.map(function(item, index) {
  return Object.assign({}, { ...item,
    uniqueId: index + 1
  })

});

console.log(n)

发布评论

评论列表(0)

  1. 暂无评论