1

I would like to know how modify array of objects in javascript.

i.e for obj change the value key array to string when value is array

change the value key string to array when value is string

also, how to convert output as input obj and (vice-versa-<input as output) in javascript

function newObject(obj){
  var result = obj.map(e=>typeof e.value === "string" ?
  {...e, value: [e.value] } :
   e.value.map(value => ({...e,value})) 
  ).flat()
  console.log(result);
  return result;

}


scenario1

input:
obj =[
 { id: 0, key: "s1", value: ["listA","listB"], img:"" },
 { id: 1, key: "s2", value: ["listC"], img: "" },
 { id: 2, key: "s3", value: ["listD","listE","listF"], img: "" }
]

Expected output:
[
  { id: 0, key: "s1", value: "listA,listB", img:""},
  { id: 1, key: "s2", value: "listC", img:""},
  { id: 2, key: "s3", value: "listD,listE,listF", img:""}
]

scenario 2

input
[
  { id: 0, key: "s1", value: "listA,listB", img:""},
  { id: 1, key: "s2", value: "listC", img:""},
  { id: 2, key: "s3", value: "listD,listE,listF", img:""}
]

Expected Output
[
 { id: 0, key: "s1", value: ["listA","listB"], img:"" },
 { id: 1, key: "s2", value: ["listC"], img: "" },
 { id: 2, key: "s3", value: ["listD","listE","listF"], img: "" }
]

3
  • What happened to listF in the last object of your output array? (first code block?) Commented Sep 4, 2020 at 12:05
  • @NickParsons thanks for reply, updated the code, was missed out Commented Sep 4, 2020 at 12:07
  • arr.forEach(item => item.value = item.value.join('') and arr.forEach(item => item.value = item.value.split(',') Commented Sep 4, 2020 at 12:09

2 Answers 2

2

When you use .map() you can return a new object with all the properties and values of the current object (...o), along with an update value property. The update value property will check if o.value is an array (checked using Array.isArray), if it is, it will .join() the elements into a string, it is a string (ie: not an array), it will .split() on the commas to convert the string into an array.

See example below:

function newObject(arr){
  return arr.map(o => ({
    ...o,
    value: Array.isArray(o.value) ? o.value.join() : o.value.split(',')
  }));
}

const arr1 = [ { id: 0, key: "s1", value: ["listA","listB"], img:"" }, { id: 1, key: "s2", value: ["listC"], img: "" }, { id: 2, key: "s3", value: ["listD","listE","listF"], img: "" } ];
const arr2 = [ { id: 0, key: "s1", value: "listA,listB", img:""}, { id: 1, key: "s2", value: "listC", img:""}, { id: 2, key: "s3", value: "listD,listE,listF", img:""} ];

const res1 = newObject(arr1);
const res2 = newObject(arr2);
console.log(res1);
console.log(res2);

Sign up to request clarification or add additional context in comments.

Comments

2

we can use for loop to achieve more performance

let arr_1 = [
  { id: 0, key: "s1", value: ["listA","listB"], img:"" },
  { id: 1, key: "s2", value: ["listC"], img: "" },
  { id: 2, key: "s3", value: ["listD","listE","listF"], img: "" },

  { id: 0, key: "s1", value: "listA,listB", img:""},
  { id: 1, key: "s2", value: "listC", img:""},
  { id: 2, key: "s3", value: "listD,listE,listF", img:""}
]


const f = (arr) => {
  for(let i = 0; i < arr.length; i++) {
    let x = arr[i].value        
    arr[i].value = Array.isArray(x) ? x.join() : x.split(',')
  }
console.log(arr)
}

f(arr_1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.