简体   繁体   English

如何将在数组中具有属性值的 object 与 pipe 连接为逗号分隔

[英]How to turn an object that has property values inside an array joined with pipe into comma separated

I have an object with values like this我有一个像这样的值的 object

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:我想把它变成一个看起来像这样的 object:

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:我以为我写的这个 function 会这样做:

  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.我认为问题可能在于 uniqueId 本身只是一个字符串,而所有其他属性都在一个数组中。 I need it to leave the string as is, but do the operations on the array values.我需要它保持字符串不变,但对数组值进行操作。

You can use a for in like this你可以像这样使用 for in

 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使用 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);

If you have mixed arrays species: ["human|alien","another","klingon|ferengi"] , the accepted answer won't work.如果您混合了 arrays species: ["human|alien","another","klingon|ferengi"] ,则接受的答案将不起作用。 Try this (using array.concat() )试试这个(使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM