0

I have the below object

{
  lookUp: {
    id: "123",
    text: {
      street_address_test: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],
    },
  },
}

I want to append the below (using JavaScript i.e nodejs to be specific)

street_address_uk: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],

so that it becomes

{
  lookUp: {
    id: "123",
    text: {
      street_address_test: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],
      street_address_uk: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],
    },
  },
}

I looked at existing Stack Overflow posts and https://stackoverflow.com/a/8889386/4068218 was close but dynamically I will not know street_address_test or street_address_uk.

I went through other Stack Overflow posts as well but most of the solutions were on arrays but as you see mine is not an array.

Any leads are much appreciated.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Hari Rao
  • 2,990
  • 3
  • 21
  • 34
  • Could you just say `jsonObj["lookUp"]["text"]["street_address_uk"] = appendObj["street_address_uk"];` ? – async await Sep 23 '21 at 22:54
  • 1
    In addition to the recommendation from @AsyncAwaitFetch you can do directly like this: `jsonObj["lookUp"]["text"]["street_address_uk"] =[{pos: 12,len: 6,},{pos: 3,len: 8,},{pos: 3, len: 15,},];` if it's a constant – Alexey Zelenin Sep 23 '21 at 23:05
  • There is no JSON in this question, only plain old arrays and objects. JSON is a text format, and requires that property names be quoted, among other differences from the literal syntax used here. – Heretic Monkey Sep 23 '21 at 23:58
  • Why is everyone using bracket syntax? Doesn't it make more sense to use `jsonObj.lookUp.text.street_address_uk`? – Heretic Monkey Sep 24 '21 at 00:02
  • @HereticMonkey in this situation it may not make much of a difference, however I tend to use the bracket syntax because if you are dynamically grabbing the name of they property, you could pass the string into the bracket, however you could not use the direct property. In this situation it may not make a difference. – async await Sep 24 '21 at 17:31

3 Answers3

1

Considering the array object received has to be assigned always to property text of lookUp object you can do the following,

let objectToAssign = {
  street_address_test: [{
      pos: 12,
      len: 6,
    },
    {
      pos: 3,
      len: 8,
    },
    {
      pos: 3,
      len: 15,
    },
  ],
  street_address_uk: [{
      pos: 12,
      len: 6,
    },
    {
      pos: 3,
      len: 8,
    },
    {
      pos: 3,
      len: 15,
    },
  ]
};

let objectToAssignTo = {
  lookUp: {
    id: "123",
    text: {},
  },
}

function assignPropToObject(object, globalObject) {
  let prop = Object.keys(object);

  for (let i = 0; i < prop.length; i++) {
    globalObject['lookUp']['text'][prop[i]] = object[prop[i]];
  }

  console.log(globalObject)
}

assignPropToObject(objectToAssign, objectToAssignTo)
thisdotutkarsh
  • 940
  • 6
  • 7
1

You can use the object spread operator, object1{...object2} and overwrite nested properties like so.

In the example below, I have assumed that you will accumulate the individual street_address arrays into a lookUpData object.

let initObject = {
  lookUp: {
    id: "123",
    text: {},
  },
};

let lookUpData = {
  street_address_test: [{
      pos: 12,
      len: 6,
    },
    {
      pos: 3,
      len: 8,
    },
    {
      pos: 3,
      len: 15,
    },
  ],
  street_address_uk: [{
      pos: 12,
      len: 6,
    },
    {
      pos: 3,
      len: 8,
    },
    {
      pos: 3,
      len: 15,
    },
  ],
};

let newObject = {
  ...initObject,
  lookUp: {
    ...initObject.lookUp,
    text: lookUpData,
  },
};

console.log(initObject);
console.log(newObject);
Alex
  • 2,164
  • 1
  • 9
  • 27
0

thank you @thisdotutkarsh and @Alex. Your answers helped me. I think I should have been more specific but I managed to get what I want. Pasted the solution below in case if somebody is looking for something similar

let master = {
  lookUp: {
    id: "123",
    text: {
       street_address_uk: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],
      
    },
  },
}

let child = {
  lookUp: {
    id: "123",
    text: {
       street_address_US: [
        {
          pos: 12,
          len: 6,
        },
        {
          pos: 3,
          len: 8,
        },
        {
          pos: 3,
          len: 15,
        },
      ],
      
    },
  },
}
const prop = Object.keys(master.lookUp.text);
const bl = Object.keys(child.lookUp.text);
master.lookUp.text[bl[bl.length-1]] =  child.lookUp.text[bl[bl.length-1]] ;
console.log(master.lookUp)
Hari Rao
  • 2,990
  • 3
  • 21
  • 34