0

I have an object with values like this

const objectValues = {
    weight: ["0|5"],
    species: ["human|alien"],
    colour: ["Blue-Green|Red|Black|Orange With Green, Red, Yellow And Blue Play Of Colour"],
    shape: ["Rough"],
    uniqueId: "kobe"
}

I want to turn this into an object that looks like this:

const desiredObject = {
    weight: ["0","5"],
    species: ["human","alien"],
    colour: ["Blue-Green","Red","Black","Orange With Green, Red, Yellow And Blue Play Of Colour"],
    shape: ["Rough"],
    uniqueId: "kobe"
}

I thought this function i wrote would do it:

  let pipeToCommaObject = {};

  const mapSplitted = Object.keys(objectValues).map(key => {
    if(objectValues[key].join().includes('|')){
      pipeToCommaObject[key] = objectValues[key].join().split('|');
    }
  });

However it's not quite doing it, please advise what i'm missing and what i need to change/add to get my desired result. I think the issue might be that uniqueId is just a string by itself where as all the other properties are in an array. I need it to leave the string as is, but do the operations on the array values.

Jim41Mavs
  • 290
  • 1
  • 14
  • Does this answer your question? [map function for objects (instead of arrays)](https://stackoverflow.com/questions/14810506/map-function-for-objects-instead-of-arrays) Some of those answers are what you need, with slightly different mapping function – Drenai May 11 '21 at 20:57
  • @Drenai not really, i don't really even need the map. I could use Object.keys(objectValues).forEach instead. I don't need to return a new array. I just need to fill the pipeToCommaObject with object property values in the shape that i want to – Jim41Mavs May 11 '21 at 21:02
  • Fair enough I see what you mean – Drenai May 11 '21 at 21:10

2 Answers2

1

You can use a for in like this

const object = {
  weight: ["0|5"],
  species: ["human|alien"],
  colour: [
    "Blue-Green|Red|Black|Orange With Green, Red, Yellow And Blue Play Of Colour",
  ],
  shape: ["Rough"],
  uniqueId: "kobe",
};

const newObject = {};

for (let key in object) {
  const property = object[key];
  //Check if element is an Array
  if (Array.isArray(property)) {
    //Split the first element
    newObject[key] = property[0].split("|");
  } else {
    newObject[key] = property;
  }

}

console.log(newObject);

The same logic using a forEach

const object = {
  weight: ["0|5"],
  species: ["human|alien"],
  colour: [
    "Blue-Green|Red|Black|Orange With Green, Red, Yellow And Blue Play Of Colour",
  ],
  shape: ["Rough"],
  uniqueId: "kobe",
};

const newObject = {};


Object.keys(object).forEach(key => {
  const property = object[key];
  //Check if element is an Array
  if (Array.isArray(property)) {
    //Split the first element
    newObject[key] = property[0].split("|");
  } else {
    newObject[key] = property;
  }
})

console.log(newObject);
Juan Rambal
  • 389
  • 1
  • 6
0

If you have mixed arrays species: ["human|alien","another","klingon|ferengi"], the accepted answer won't work. Try this (using array.concat())

const objectValues = {
        weight: ["0|5"],
        species: ["human|alien","another","klingon|ferengi"],
        colour: ["Blue-Green|Red|Black|Orange With Green, Red, Yellow And Blue Play Of Colour"],
        shape: ["Rough"],
        uniqueId: "kobe"
    }
    
    let desiredObject={}
    for(let item in objectValues) {
        if(Array.isArray(objectValues[item])) {
            tmp = []
            objectValues[item].forEach(el => tmp=tmp.concat(el.split("|")));
           desiredObject[item] = tmp
        } else desiredObject[item] = objectValues[item]
    }
    console.log(desiredObject);
Kinglish
  • 4,042
  • 2
  • 15
  • 27