Search code examples
javascriptarraysecmascript-6ecmascript-5

Find and replace required object properties with values with respect to array of objects


Apologise if you feel the question is a bit lengthy. I have an array of objects in the following structure:

let arrObj = [{
        id: "test1",
        value1: "a1",
        value2: "b1",
    },
    {
        id: "test2",
        value1: "a2",
        value2: "b2",
    },
    {
        id: "test3",
        value1: "a3",
        value2: "b3",
    },
    {
        id: "test4",
        value1: "a4",
        value2: "b4",
    }
];

I basically have to replace object with id test2, test3,test4 based on some input passed, rest objects should be untouched. I have written a switch case that takes three values and then computes and then returns modified array of objects.

The values that are being passed on which modification should happen are abc,def,ghi.

Basically following values needs to be returned based on the input values The value1,value2 are just some hardcoded values

var res1 = updateValue(arrObj,"abc");
//result should be
 [{
        id: "test1",
        value1: "a1",
        value2: "b1",
    },
    {
        id: "test2",
        value1: "dsdsd",
        value2: "ghasgas",
    },
    {
        id: "test3",
        value1: "dsds",
        value2: "asasas",
    },
    {
        id: "test4",
        value1: "dsdsdsae",
        value2: "saheuwe",
    }
];

In similar way,

var res1 = updateValue(arrObj,"def");
//result should be
[{
        id: "test1",
        value1: "a1",
        value2: "b1",
    },
    {
        id: "test2",
        value1: "dshds67",
        value2: "sdjsahdj1213",
    },
    {
        id: "test3",
        value1: "jdshdjh123",
        value2: "dkshdksj88",
    },
    {
        id: "test4",
        value1: "hjwhekjwq2123",
        value2: "sjhdj2323",
    }
];

Also,

var res3 = updateValue(arrObj,"ghi");
//result should be
[{
        id: "test1",
        value1: "a1",
        value2: "b1",
    },
    {
        id: "test2",
        value1: "gahsddct21ew",
        value2: "gdsaedtsasa2re",
    },
    {
        id: "test3",
        value1: "gdsdssselectdsd",
        value2: "ghiasaselect3we",
    },
    {
        id: "test4",
        value1: "ghdsiselectdsdre",
        value2: "ghdsiselectr4"
    }
];

Code that I have tried:

function updateValue(obj, value) {
    let defaultObj = {
        id: ""
    }
    var newObj = {};
    switch (value) {
        case "abc":
            newObj.value1 = "abcselect21"
            newObj.value2 = "abcselect22"
            break;

        case "def":
            newObj.value1 = "defselect21";
            newObj.value2 = "defselect22"
            break;

        case "ghi":
            newObj.value1 = "ghiselect21";
            newObj.value2 = "ghiselect22"
            break;
    }

    finalArr = [{
        ...defaultObj,
        ...newObj
    }];
    return finalArr;
}

Solution

  • It's always challenging to answer questions where the data is simplified to a degree that it hides your original intention. Anyway, here's how I might break the problem down -

    // generics
    const identity = x => x
    
    const update1 = (o = {}, [ k = "", t = identity ]) =>
      ({ ...o, [k]: t(o[k]) })
      
    const update = (o = {}, patch = {}) =>
      Object.entries(patch).reduce(update1, o)
    
    // specifics
    const transform = ([ defaults, ...more ], str = "") =>
      [ defaults
      , ...more.map(item =>
          update(item, {
            value1: v => `${str}select${getId(v)}1`,
            value2: v => `${str}select${getId(v)}2`
          })
        )
      ]
    
    const getId = (s = "") =>
      s.match(/\d+/) || 0
    
    // test
    const arrObj =
      [ {id:"test1",value1:"a1",value2:"b1"}
      , {id:"test2",value1:"a2",value2:"b2"}
      , {id:"test3",value1:"a3",value2:"b3"}
      , {id:"test4",value1:"a4",value2:"b4"}
      ]
      
    const result =
      transform(arrObj, "xyz")
      
    console.log(result)