Robin Singh
Robin Singh

Reputation: 1796

How to add object to specific place in JavaScript?

I am using a library React simple chatbot and add data from database, but the problem is i have array like below

Array from Database

[{
    "id": 1,
    "message": "What is your name?",
    "type": "text",
    "trigger": "2"
}, {
    "id": 2,
    "message": "What is your age?",
    "type": "text",
    "trigger": "3"
}, {
    "id": 3,
    "message": "What is your date of birth?",
    "type": "text",
    "trigger": "1"
}]

But i need something like below

[{
    "id": 1,
    "message": "What is your name?",
    "type": "text",
    "trigger": "2"
}, {
    "id": 2,
    "user": true,
    "trigger": 3
}, {
    "id": 3,
    "message": "What is your age?",
    "type": "text",
    "trigger": "4"
}, {
    "id": 4,
    "user": true,
    "trigger": 5
}, {
    "id": 5,
    "message": "What is your date of birth?",
    "type": "text",
    "trigger": "6"
}, {
    "id": 6,
    "user": true,
    "trigger": 1
}]

Please help me to create array like this. I write some code, but doesn't work for me. My code print duplicate ids. Basically i want to pass object after every object with increment id.

Code

  fetch("api_url_here")
        .then((res) => {
          return res.json();
        })
        .then((data) => {
          const steps = data.map((res, index) => {
            return {
              id: index,
              message: res.question,
              type: res.type,
              trigger: res.trigger,
            };
          });
          var array = [];

          var trigger = 1;
          steps.forEach((step, index) => {
            index = index;
            array.push(step);
            if (step.type == "text") {
              array.push({
                id: trigger + 1,
                user: true,
                trigger: trigger,
              });
            }
            trigger++;
          });
          document.getElementById("json").append(JSON.stringify(array));
        })

Current Output

[{
    "id": 0,
    "message": "What is your name?",
    "type": "text",
    "trigger": "2"
}, {
    "id": 2,
    "user": true,
    "trigger": 1
}, {
    "id": 1,
    "message": "What is your age?",
    "type": "text",
    "trigger": "3"
}, {
    "id": 3,
    "user": true,
    "trigger": 2
}, {
    "id": 2,
    "message": "What is your date of birth?",
    "type": "text",
    "trigger": "1"
}, {
    "id": 4,
    "user": true,
    "trigger": 3
}]

Any solution appreciated!

Upvotes: 1

Views: 65

Answers (1)

malarres
malarres

Reputation: 2946

You only need to add this as a final step to correct the id and trigger

const almost = [{
  "id": 0,
  "message": "What is your name?",
  "type": "text",
  "trigger": "2"
}, {
  "id": 2,
  "user": true,
  "trigger": 1
}, {
  "id": 1,
  "message": "What is your age?",
  "type": "text",
  "trigger": "3"
}, {
  "id": 3,
  "user": true,
  "trigger": 2
}, {
  "id": 2,
  "message": "What is your date of birth?",
  "type": "text",
  "trigger": "1"
}, {
  "id": 4,
  "user": true,
  "trigger": 3
}]

const final = almost.map( (obj,index) => {
  const newObj = {};
  Object.assign(newObj,
    obj,
    {
      "id":index+1,
      "trigger": index+2
    }
  );
  return newObj;
});
final[final.length-1].trigger = 1;

console.log(final)

EDIT: to make it clear, your code would look like:

fetch("api_url_here")
        .then((res) => {
          return res.json();
        })
        .then((data) => {
          const steps = data.map((res, index) => {
            return {
              id: index,
              message: res.question,
              type: res.type,
              trigger: res.trigger,
            };
          });
          var array = [];

          var trigger = 1;
          steps.forEach((step, index) => {
            index = index;
            array.push(step);
            if (step.type == "text") {
              array.push({
                id: trigger + 1,
                user: true,
                trigger: trigger,
              });
            }
            trigger++;
          });
          
          array = array.map( (obj,index) => {
          const newObj = {};
          Object.assign(newObj,
            obj,
            {
              "id":index+1,
              "trigger": index+2
            }
          );
          return newObj;
        });
          array[array.length-1].trigger = 1;
          
          
          document.getElementById("json").append(JSON.stringify(array));
        })

Upvotes: 1

Related Questions