skip to Main Content

we have an object called options. I wanted to append in options those values, which are not present from object2 based on id.

options
0: {value: '642a60e2573c9d004b041407', label: 'label1', mapped: true}
1: {value: '642a60e2573c9d004b041408', label: 'label2', mapped: true}
2: {value: '642a60e2573c9d004b041409', label: 'label3', mapped: true}
3: {value: '642a60e2573c9d004b04140a', label: 'label4', mapped: true}
4: {value: '642a60e2573c9d004b04140b', label: 'label5 ', mapped: true}
5: {value: '642a60e2573c9d004b04140c', label: 'label6', mapped: true}


object2 = 
642a60e2573c9d004b04140a: {id: '642a60e2573c9d004b04140a', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null}
642a60e2573c9d004b04140b: {id: '642a60e2573c9d004b04140b', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null}
642a60e2573c9d004b04140c: {id: '642a60e2573c9d004b04140c', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null,}
642a60e2573c9d004b04140d: {id: '642a60e2573c9d004b04140d', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null, }
642a60e2573c9d004b04140e: {id: '642a60e2573c9d004b04140e', createdAt: '2023-04-03T05:15:14.317Z', myApiName: null, abel: null, }
642a60e2573c9d004b04140f: {id: '642a60e2573c9d004b04140f', createdAt: '2023-04-03T05:15:14.317Z', myApiName: null, abel: null, }
642a60e2573c9d004b041407: {id: '642a60e2573c9d004b041407', createdAt: '2023-04-03T05:15:14.315Z', myApiName: null, abel: null, }
642a60e2573c9d004b041408: {id: '642a60e2573c9d004b041408', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null, }
642a60e2573c9d004b041409: {id: '642a60e2573c9d004b041409', createdAt: '2023-04-03T05:15:14.316Z', myApiName: null, abel: null, }
642a6123573c9d0024a992f2: {id: '642a6123573c9d0024a992f2', createdAt: '2023-04-03T05:16:19.896Z', myApiName: null, abel: null, }

Since, I am new to JS, any help would be appreciated.

Expected result:

options
        0: {value: '642a60e2573c9d004b041407', label: 'label1', mapped: true}
        1: {value: '642a60e2573c9d004b041408', label: 'label2', mapped: true}
        2: {value: '642a60e2573c9d004b041409', label: 'label3', mapped: true}
        3: {value: '642a60e2573c9d004b04140a', label: 'label4', mapped: true}
        4: {value: '642a60e2573c9d004b04140b', label: 'label5 ', mapped: true}
        5: {value: '642a60e2573c9d004b04140c', label: 'label6', mapped: true}
        6: {value: '642a6123573c9d0024a992f2', label: 'label7', mapped: true}
        7: {value: '642a60e2573c9d004b04140d', label: 'label5 ', mapped: true}
        8: {value: '642a60e2573c9d004b04140e', label: 'label6', mapped: true}
        9: {value: '642a60e2573c9d004b04140f', label: 'label7', mapped: true}```

2

Answers


  1. You can map over the options object and add elements from the other object. This might solve your problem.

    const optionsMap = options.map(option => {
      return {
        ...option,
        ...object2.find(obj => obj.id === option.value)
      }
    });
    
    options = [{
      value: '642a60e2573c9d004b041407',
      label: 'label1',
      mapped: true
    }, {
      value: '642a60e2573c9d004b041408',
      label: 'label2',
      mapped: true
    }, {
      value: '642a60e2573c9d004b041409',
      label: 'label3',
      mapped: true
    }, {
      value: '642a60e2573c9d004b04140a',
      label: 'label4',
      mapped: true
    }, {
      value: '642a60e2573c9d004b04140b',
      label: 'label5 ',
      mapped: true
    }, {
      value: '642a60e2573c9d004b04140c',
      label: 'label6',
      mapped: true
    }, ]
    
    object2 = [{
        id: '642a60e2573c9d004b04140a',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null
      }, {
        id: '642a60e2573c9d004b04140b',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null
      }, {
        id: '642a60e2573c9d004b04140c',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b04140d',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b04140e',
        createdAt: '2023-04-03T05:15:14.317Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b04140f',
        createdAt: '2023-04-03T05:15:14.317Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b041407',
        createdAt: '2023-04-03T05:15:14.315Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b041408',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a60e2573c9d004b041409',
        createdAt: '2023-04-03T05:15:14.316Z',
        myApiName: null,
        abel: null,
      },
      {
        id: '642a6123573c9d0024a992f2',
        createdAt: '2023-04-03T05:16:19.896Z',
        myApiName: null,
        abel: null,
      }
    ];
    
    const optionsMap = options.map(option => {
      return {
        ...option,
        ...object2.find(obj => obj.id === option.value)
      }
    });
    console.log('result', optionsMap);
    Login or Signup to reply.
  2. I found this solution, which is working, you can refer this.

    console.log(options);
    var addNewItem = true;
    Object.entries(object2).forEach((ele, index) => {
        addNewItem = true;
        Object.entries(options).forEach((item, inx) => {
            if (item[1].value == ele[1].id) {
                addNewItem = false;
                return false;
            }
        })
        if (addNewItem) {
            var keyVal = Object.keys(options).length;
            let newobj={[keyVal]: { value: ele[1].id, label: "newLable", mapped: true }}
            options = Object.assign(options, newobj)
        }
    })
    
    console.log(options);
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search