Mindaugas Inc
Mindaugas Inc

Reputation: 49

changing object keys acording to an array

i have an object and 2 arrays. i want to change object keys according to arr2, object keys and arr1 values are identical while arr1 and arr2 placement of values in arrays are identical but not values themselves, while i don't want to ask how to solve it want to know how should i approach this problem, thank you for your time

    const object = {
        name1: "some value",
        name3: "some value",
        name2: "some value",
        etc...}
    const arr1 = ["name1", "name2", "name3"]
    const arr2 = ["Name1", "Name2", "Name3"]

expected outcome

    const newObject = {
        Name1: "some value",
        Name3: "some value",
        Name2: "some value",
        etc...}

Upvotes: 3

Views: 94

Answers (6)

Kobe
Kobe

Reputation: 6457

You can use reduce:

const object = {
  name1: "some value",
  name3: "some value",
  name2: "some value",
}
const arr1 = ["name1", "name2", "name3"]
const arr2 = ["Name1", "Name2", "Name3"]

const newObj = arr1.reduce((acc, value, index) => {
   acc[arr2[index]] = object[value]
   return acc
}, {})

console.log(newObj)

Reduce will loop over the array 1, use the same index from the second array to assign the key, and compare the value from the first array against the key from the object, and then assign it to the new object.

If readability isn't an issue, you can write the reduce in one line:

const object = {
  name1: "some value",
  name3: "some value",
  name2: "some value",
}
const arr1 = ["name1", "name2", "name3"]
const arr2 = ["Name1", "Name2", "Name3"]

const newObj = arr1.reduce((a, v, i) => (a[arr2[i]] = object[v], a), {})

console.log(newObj)

Upvotes: 3

Akrion
Akrion

Reputation: 18525

You can one line this via Array.map and then Object.fromEntries:

  const object = {
    name1: "some value A",
    name3: "some value B",
    name2: "some value C"
  }
  const old = ["name1", "name2", "name3"]
  const keys = ["Name1", "Name2", "Name3"]

let result = Object.fromEntries(old.map((x,i) => [keys[i], object[x]]))

console.log(result)

Upvotes: 1

Shidersz
Shidersz

Reputation: 17190

Assuming you want to preserve keys names not present on arr1 on the new object. Then you can first create a Map between old names and new names (In case ES6 is not an option for you, use an object here for the mapping). Then you can traverse the Object.entries() with Array.reduce() to generate the new object, where only the related keys names are mapped to new ones, and the rest are preserved.

const obj = {
  name1: "some value 1",
  name3: "some value 2",
  name2: "some value 3",
  name4: "some value 4",
  name5: "some value 5"
}

const arr1 = ["name1", "name2", "name3"];
const arr2 = ["Name1", "Name2", "Name3"];

// Create a map between old names and new names.
let keysMap = new Map(arr1.map((key, idx) => [key, arr2[idx]]));

// Generate the new object, preserving non-mapped keys names.
let newObj = Object.entries(obj).reduce((acc, [k, v]) =>
{
    acc[keysMap.has(k) ? keysMap.get(k) : k] = v;
    return acc;
}, {});

console.log(newObj);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Upvotes: 1

gixlg
gixlg

Reputation: 1355

You can simply do this:

var output={};
arr1.forEach(function(el, index){
   output[arr2[index]] = object[el];
}, this);

Upvotes: 2

Maheer Ali
Maheer Ali

Reputation: 36594

You can use reduce()

const object = {
        name1: "some value",
        name3: "some value",
        name2: "some value"
}

const arr1 = ["name1", "name2", "name3"]
const arr2 = ["Name1", "Name2", "Name3"]

const res = arr1.reduce((ac,a,i) => (ac[arr2[i]] = object[a],ac),{});
console.log(res)

Upvotes: 2

Vishal Kumar
Vishal Kumar

Reputation: 340

I assume you are mapping them by positions in your arr1 and arr2. Just run a loop.

 const oldObject = {
        name1: "some value",
        name3: "some value",
        name2: "some value",
};

const arr1 = ["name1", "name2", "name3"];
const arr2 = ["Name1", "Name2", "Name3"];

var newObj = {};

for (var i = 0; i < arr2.length; i++) {
  newObj[arr2[i]] = oldObject[arr1[i]];
}

console.log(newObj);

Upvotes: 6

Related Questions