如何将逗号分隔的字符串转换为对象中的单独数组?

时间:2017-04-04 12:38:51

标签: javascript arrays string object

let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";

如何使用javascript将上面的字符串转换为对象中的单独数组,如:

{
  dress = ["cotton","leather","black","red","fabric"];
  houses = ["restaurant","school","small","big"];
  person = ["james"];
}

8 个答案:

答案 0 :(得分:0)

拆分字符串!

//The Entry String
myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";

//The first Array
var objectArray = myStr.split(',');

//Loop trough each item of the array
for(var i in objectArray){
    //The final sub-array
    objectArray[i] = objectArray[i].split('/');
}

答案 1 :(得分:0)

可能的方法,使用Array#forEach



let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james".split(', '), obj = {};

myStr.forEach(function(v){
   (obj[v.split('/')[0]] || (obj[v.split('/')[0]] = [])).push(...v.split('/').slice(1));
});

console.log(obj);




答案 2 :(得分:0)

您可以使用split()获取元素数组,然后在该数组上使用reduce()来构建对象。



let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";

var result = myStr.split(', ').reduce(function(r, a) {
  var e = a.split('/');
  if (!r[e[0]]) r[e[0]] = []
  r[e[0]].push(...e.slice(1));
  return r
}, {})

console.log(result)




答案 3 :(得分:0)

您可以将字符串分组,然后分成值。将第一个作为valus的键,并将值连接到旧项或新数组。然后用密钥将其分配给对象。

var string = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james",
    result = {};
    
string.split(', ').forEach(function (a) {
    var values = a.split('/'),
        key = values.shift();
    result[key] = (result[key] || []).concat(values);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:0)

您可以这样做:



let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";

var obj = myStr.split(", ").reduce((ret, val) => {
   var arr = val.split("/");
   if(ret[arr[0]]){
     ret[arr[0]].push(...arr.filter((v, i) => i!=0));    
   }else{
     ret[arr[0]] = arr.filter((v, i) => i!=0);
   }
   return ret;
}, {});
console.log(obj);




答案 5 :(得分:0)



let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";

let res = myStr.split(', ').reduce((acc, s) => {
  let parts = s.split('/'),
      cat = parts.shift();
  if(acc[cat])
    acc[cat] = acc[cat].concat(parts);
  else
    acc[cat] = parts;
  return acc;
}, {});

console.log(res);




答案 6 :(得分:0)

let myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";
let output = {};

myStr.replace(/\s*,\s*/gi, ",").split(",").forEach(item => {
let itemArr = item.split("/");

if (itemArr && itemArr.length) {
    var key = itemArr[0];

    output[key] = (output.hasOwnProperty(key)) ?
        output[key].concat(itemArr.slice(1, itemArr.length)) : itemArr.slice(1, itemArr.length);

}
  
});

console.log(output);

答案 7 :(得分:0)

var myStr = "dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james";
var Arr = myStr.split(', ');
var keyJSON = new Array();
$.each(Arr,function(i,o){
var key = o.split('/')[0];
keyJSON.push(key);
$.unique(keyJSON);
});
var finalArr = new Array();
$.each(keyJSON,function(keyI,keyO){
    finalArr[keyO] = new Array();
});
$.each(Arr,function(i,o){
    var innerArr = o.split('/');
    for(var ind=1;ind<innerArr.length;ind++){
        finalArr[innerArr[0]].push(innerArr[ind]);
    }
});