Jack The Baker
Jack The Baker

Reputation: 1883

Add new object based on key:value

I have an object, I want to add a new object before each object based on did value. What I tried is below, but it not what I want, it add for each item, and also it turned into array.

let obj = {
  "district": [{
      "id": 1,
      "uid": 1,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 1,
      "name": "text 1"
    },
    {
      "id": 2,
      "uid": 2,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 2,
      "name": "text 2"
    },
    {
      "id": 3,
      "uid": 3,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 2,
      "name": "text 3"
    },
    {
      "id": 4,
      "uid": 4,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 3,
      "name": "text 4"
    },
    {
      "id": 5,
      "uid": 5,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 3,
      "name": "text 5"
    },
    {
      "id": 6,
      "uid": 6,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 0, // should not add object before this becaus did is 0
      "name": "text 6"
    }
  ]
}


var result = obj.district.map(function(el) {
if(el.did > 0){
  var o = Object.assign({}, obj.district);
  o.divider = {
    "dv": true,
    "name": 'divider ' + el.did
  };
  return o;
}
})

console.log(result)

Add new object if did value is not null or 0, kinda > 0. the result should be like this:

let obj = {
  "district": [{
      "dv": true,
      "name": "divider 1"
    }, {
      "id": 1,
      "uid": 1,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 1,
      "name": "text 1"
    },
    {
      "dv": true,
      "name": "divider 2"
    },
    {
      "id": 2,
      "uid": 2,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 2,
      "name": "text 2"
    },
    {
      "id": 3,
      "uid": 3,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 2,
      "name": "text 3"
    },
    {
      "dv": true,
      "name": "divider 3"
    },
    {
      "id": 4,
      "uid": 4,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 3,
      "name": "text 4"
    },
    {
      "id": 5,
      "uid": 5,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 3,
      "name": "text 5"
    },
    {
      "id": 6,
      "uid": 6,
      "type": 3,
      "pid": 0,
      "cid": 0,
      "did": 0, // should not add object before this becaus did is 0
      "name": "text 6"
    }
  ]
}

console.log(obj)

It should add before object with common did id, for example if there is 5 item with did 2, it should just add new object once, not for each item. also name of new value should be based on did id, divider 1, divider 2 or ..

Upvotes: 0

Views: 111

Answers (1)

saulotoledo
saulotoledo

Reputation: 1982

Try this:

var lastDivider;
var result = {
  district: []
}

obj.district.forEach(function(el) {
  if (el.did > 0 && lastDivider !== el.did) {
    result.district.push({
      "dv": true,
      "name": 'divider ' + el.did
    });
    lastDivider = el.did;
  }
  result.district.push([Object.assign({}, el)]);
});

Upvotes: 1

Related Questions