-1

I am wanting to loop through an object and make some changes to the data. Im not 100% if im actually dealing with an object or an array that contains multiple object so any help appreciated.

Here is my object:

var screening = {
    source: "jacket",
    value: {
        Cyan: {
            dotshape: "C",
            frequency: 120,
            angle: 67.5
        },
        Magenta: {
            dotshape: "C",
            frequency: 120,
            angle: 37.5
        },
        Yellow: {
            dotshape: "C",
            frequency: 120,
            angle: 82.5
        },
        "PANTONE 3528 C": {
            dotshape: "C",
            frequency: 120,
            angle: 7.5
        },
        "PANTONE 293 C": {
            dotshape: "C",
            frequency: 120,
            angle: 7.5
        },
        "PANTONE 2748 C": {
            dotshape: "C",
            frequency: 120,
            angle: 7.5
        },
        Varnish: {
            dotshape: "C",
            frequency: 120,
            angle: 0
        }
    }
}

Inside i want to essentially do a find and replace of the 'keys'(again not sure on the terminology) based on 2 lists.

Old = [Cyan, Magenta, Yellow]
new = [changedName1, changedName2, changedName3]

Ideally i want the loop to look at the current object, if it has a match to something in the array Old then rename it as per the New. If there is no match to something in the array Old then just ignore it and leave it as it is.

Thanks in advance

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79

1 Answers1

0

Use delete to delete the old properties, and use [] (square bracket notation) and indexOf to get the old ones and create new ones:

var screening = {
  source: "jacket",
  value: {
    Cyan: {
      dotshape: "C",
      frequency: 120,
      angle: 67.5
    },
    Magenta: {
      dotshape: "C",
      frequency: 120,
      angle: 37.5
    },
    Yellow: {
      dotshape: "C",
      frequency: 120,
      angle: 82.5
    },
    "PANTONE 3528 C": {
      dotshape: "C",
      frequency: 120,
      angle: 7.5
    },
    "PANTONE 293 C": {
      dotshape: "C",
      frequency: 120,
      angle: 7.5
    },
    "PANTONE 2748 C": {
      dotshape: "C",
      frequency: 120,
      angle: 7.5
    },
    Varnish: {
      dotshape: "C",
      frequency: 120,
      angle: 0
    }
  }
};

function oldToNew(oldNames, newNames) {
  Object.entries(screening.value).forEach(([key, prop]) => {
    if (oldNames.includes(key)) {
      screening.value[newNames[oldNames.indexOf(key)]] = screening.value[key];
      delete screening.value[key];
    }
  });
}

oldToNew(["Cyan", "Magenta", "Yellow"], ["Blue", "Purple", "Orange"]);

console.log(screening.value);

Note: The properties will not be in order, because as shown in the ECMAScript Specification:

An ECMAScript object is an unordered collection of properties

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Hi JackThats brilliant thansk so much. Im extremely new to JS but learnign all the time. I can follow the IF statement but i cant make sense of the line before Object.entries(screening.value).forEach(([key, prop]) .. what is the prop doing? When i run the code i get the following error even though it seems to work perfectly above 1 error :6: ReferenceError: Invalid left-hand side in assignment 1 info (from 42 to 43): Object.entries(screening.value).forEach(([key, prop]) => { 1 info ReferenceError: Invalid left-hand side in assignment 1 error Script Execution Error – Darren Whitfield Feb 21 '19 at 08:22