如果Object属性具有某些分隔符,如何拆分JavaScript数组元素

时间:2018-05-15 01:58:29

标签: javascript arrays json parsing split

我有一个对象数组,其中一些对象的属性中包含逗号。我想要做的是,如果该对象具有包含逗号的属性,我想将其拆分为新对象,并递归地将所有其他属性复制到新的数组元素中。

实施例: 我需要转换这个对象数组:

[ { 
    prop1: ' text1 , text2 , text3 ',
    prop2: 'stuff1',
    prop3: 'stuff1',
    prop4: 'stuff1',
    prop5: 'https://www.stuff1.com' },

  { 
    prop1: ' text1 , text2 , text3 ',
    prop2: 'stuff2',
    prop3: 'stuff2',
    prop4: 'stuff2',
    prop5: 'https://www.awefewfew.com' },

 ]

到此:

[ { 
    prop1: 'text1',
    prop2: 'stuff1',
    prop3: 'stuff1',
    prop4: 'stuff1',
    prop5: 'https://www.stuff1.com' },

    { 
    prop1: 'text2',
    prop2: 'stuff1',
    prop3: 'stuff1',
    prop4: 'stuff1',
    prop5: 'https://www.stuff1.com' },

    { 
    prop1: 'text3 ',
    prop2: 'stuff1',
    prop3: 'stuff1',
    prop4: 'stuff1',
    prop5: 'https://www.stuff1.com' },   

  { 
    prop1: 'text1',
    prop2: 'stuff2',
    prop3: 'stuff2',
    prop4: 'stuff2',
    prop5: 'https://www.awefewfew.com' },

      { 
    prop1: 'text2',
    prop2: 'stuff2',
    prop3: 'stuff2',
    prop4: 'stuff2',
    prop5: 'https://www.awefewfew.com' },

      { 
    prop1: 'text3',
    prop2: 'stuff2',
    prop3: 'stuff2',
    prop4: 'stuff2',
    prop5: 'https://www.awefewfew.com' },  
 ]

通过在prop1处拆分,然后递归地将所有其他属性复制到新数组元素中。

编辑: 我能够在Google表格中找到它,但无法将其转移到香草JS上:

function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
  var output = [];
  for (i in anArray){ 
    var splitArray = anArray[i][splitColumnIndex].split(","); 
    for (j in splitArray){ 
      var row = anArray[i].slice(0); 
      row[splitColumnIndex] = alltrim(splitArray[j]); 
      output.push(row); 
    }
  }
  return output;
}

function alltrim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

4 个答案:

答案 0 :(得分:1)

使用.reduce迭代输入,将prop1拆分为,,然后将每个输出添加到输出数组:

const input=[{prop1:' text1 , text2 , text3 ',prop2:'stuff1',prop3:'stuff1',prop4:'stuff1',prop5:'https://www.stuff1.com'},{prop1:' text1 , text2 , text3 ',prop2:'stuff2',prop3:'stuff2',prop4:'stuff2',prop5:'https://www.awefewfew.com'},]
const ouput = input.reduce((accum, { prop1, ...rest }) => {
  const prop1s = prop1.trim().split(' , ');
  prop1s.forEach(prop1 => accum.push({
    prop1,
    ...rest
  }));
  return accum;
}, []);
console.log(ouput);

答案 1 :(得分:1)

使用Model.primary_keyreduce以及split

forEach

答案 2 :(得分:1)

我喜欢array.concat结合reduce()map()的简洁性,但显然有很多方法可以做到这一点。目前尚不清楚你的prop1字符串是否总是有空格,所以这会使用一个小的正则表达式去除它们。

let arr = [ { prop1: ' text1 , text2 , text3 ',prop2: 'stuff1',prop3: 'stuff1',prop4: 'stuff1',prop5: 'https://www.stuff1.com' },{ prop1: ' text1 , text2 , text3 ',prop2: 'stuff2',prop3: 'stuff2',prop4: 'stuff2',prop5: 'https://www.awefewfew.com' },]

let final = arr.reduce((a, {prop1, ...obj}) => a.concat(
        prop1.trim()
        .split(/\s*,\s*/)
        .map(prop => ({prop1: prop, ...obj}))
    ), [])

console.log(final)

答案 3 :(得分:1)

试试这个:

var jsonObj = [{ 
    prop1: ' text1 , text2 , text3 ',
    prop2: 'stuff1',
    prop3: 'stuff1',
    prop4: 'stuff1',
    prop5: 'https://www.stuff1.com' },
  { 
    prop1: ' text1 , text2 , text3 ',
    prop2: 'stuff2',
    prop3: 'stuff2',
    prop4: 'stuff2',
    prop5: 'https://www.awefewfew.com' }];
    
 var newArr = [];   
 
 for (var i in jsonObj) {
   var splitString = jsonObj[i].prop1.split(',');
   
   splitString.map(item => {
     newArr.push({
       "prop1": item,
       "prop2": jsonObj[i].prop2,
       "prop3": jsonObj[i].prop3,
       "prop4": jsonObj[i].prop4,
       "prop5": jsonObj[i].prop5
     });
   });
 }
 
 console.log(newArr);