admin管理员组

文章数量:1325550

I have a json object as following :

I would like for each entry that has the attribute title to add a unique id, so my json object would look like this :

and in another version I want to add a unique id for each entry, so it would look like this:

How can I do that ?

Edit:

This is my json object :

I have a json object as following :

I would like for each entry that has the attribute title to add a unique id, so my json object would look like this :

and in another version I want to add a unique id for each entry, so it would look like this:

How can I do that ?

Edit:

This is my json object : https://api.myjson./bins/59prd

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 5, 2016 at 10:33 Renaud is Not Bill GatesRenaud is Not Bill Gates 2,08438 gold badges115 silver badges212 bronze badges 4
  • use for ... in to loop over the object and add an identifier. – Luuuud Commented Dec 5, 2016 at 10:36
  • 4 please add the code/objects as text, not as image. – Nina Scholz Commented Dec 5, 2016 at 10:38
  • @NinaScholz, please check my edit – Renaud is Not Bill Gates Commented Dec 5, 2016 at 10:46
  • @YukinaSpoonatte, please have a look here: minimal reproducible example – Nina Scholz Commented Dec 5, 2016 at 10:47
Add a ment  | 

2 Answers 2

Reset to default 4

You can use for...in to loop over the object and add unique identifiers.

var iterator = 0; // this is going to be your identifier

function addIdentifier(target){
  target.id = iterator;
  iterator++;
}

function loop(obj){

  for(var i in obj){

    var c = obj[i];        

    if(typeof c === 'object'){

      if(c.length === undefined){

        //c is not an array
        addIdentifier(c);

      }

      loop(c);

    }

  }

}

loop(json); // json is your input object

You could use a closure addId for the callback for iterateing the array and use the inner callback iter for nested arrays.

With the call addId, you can specify an index to start with.

function addId(id) {
    return function iter(o) {
        if ('title' in o) {
            o.id = id++;
        }
        Object.keys(o).forEach(function (k) {
            Array.isArray(o[k]) && o[k].forEach(iter);
        });
    };
}

var data = [{ "Arts": [{ "Performing arts": [{ "Music": [{ "title": "Acpanying" }, { "title": "Chamber music" }, { "title": "Church music" }, { "Conducting": [{ "title": "Choral conducting" }, { "title": "Orchestral conducting" }, { "title": "Wind ensemble conducting" }] }, { "title": "Early music" }, { "title": "Jazz studies" }, { "title": "Musical position" }, { "title": "Music education" }, { "title": "Music history" }, { "Musicology": [{ "title": "Historical musicology" }, { "title": "Systematic musicology" }] }, { "title": "Ethnomusicology" }, { "title": "Music theory" }, { "title": "Orchestral studies" }, { "Organology": [{ "title": "Organ and historical keyboards" }, { "title": "Piano" }, { "title": "Strings, harp, oud, and guitar" }, { "title": "Singing" }, { "title": "Strings, harp, oud, and guitar" }] }, { "title": "Recording" }] }, { "Dance": [{ "title": "Choreography" }, { "title": "Dance notation" }, { "title": "Ethnochoreology" }, { "title": "History of dance" }] }, { "Television": [{ "title": "Television studies" }] }, { "Theatre": [{ "title": "Acting" }, { "title": "Directing" }, { "title": "Dramaturgy" }, { "title": "History" }, { "title": "Musical theatre" }, { "title": "Playwrighting" }, { "title": "Puppetry" }] }] }] }];

data.forEach(addId(1))
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

本文标签: javascriptadd a unique id for each entry in json objectStack Overflow