0

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 ..

7
  • 2
    Mixing two different kinds data in a single array, that's not very cash money and I have never encountered a need to do something like this. Sure, that's possible, but do you really think this is the right way? Commented Feb 17, 2020 at 10:24
  • @RoryMcCrossan Second snippet exactly the output that what I want Commented Feb 17, 2020 at 10:26
  • @AbanaClara What I trying to do is kinda group each item, but I don't want to group by object, just want to add an object before each them Commented Feb 17, 2020 at 10:27
  • 2
    Grouping each item by array order is NOT grouping at all. This is a recipe for confusion. Each item in the original array is identified via the id property, why not use that instead and create a new array containing that unique identifier? Commented Feb 17, 2020 at 10:28
  • @AbanaClara containing that unique identifier? I have no idea what result will be output | maybe I choose wrong idea to do this, but I think this format is only way that solve my issue. what I really want is divide item based on did | did => mean s=> district id, I want to use this for drop down, and my drop down not support optgroup Commented Feb 17, 2020 at 10:36

1 Answer 1

1

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)]);
});
Sign up to request clarification or add additional context in comments.

3 Comments

One problem, it add divider before each item, but as I said in question it should just add new object once, not for each item
@tourtravel I updated my answer with your comment. Just consider the comment about flat. :)
@tourtravel I changed the example again with a version without flat. Please check if this is what you are looking for. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.