str = "books:book1, books:book2, houses:house1, houses:house2, person:james";
我想将上面的字符串转换为对象中的单独数组或数组,如:
books = ["book1","book2"];
houses = ["house1","house2"];
person = ["james"];
我如何在JavaScript中执行此操作?
答案 0 :(得分:2)
var string = "books:book1, books:book2, houses:house1, houses:house2, person:james";
var hash = {};
var splitByComma = string.split(',');
function addKeyByVal( key, val ){
if(!hash[key]) hash[key] = [];
hash[key].push(val);
}
for( var s in splitByComma ){
var splitByColon = splitByComma[s].split(":");
addKeyByVal(splitByColon[0], splitByColon[1]);
}
console.log(hash);
答案 1 :(得分:0)
您可以在', '
上拆分并缩小为包含key: []
的对象:
const string = "books:book1, books:book2, houses:house1, houses:house2, person:james";
const output = string.split(', ').reduce((obj, cur) => {
let [category, value] = cur.split(':');
if (!Array.isArray(obj[category])) {
obj[category] = [value];
} else {
obj[category].push(value);
}
return obj;
}, {});
console.log(output);
答案 2 :(得分:0)
您可能想要一个对象,其中包含包含数组的键。
var str = "books:book1, books:book2, houses:house1, houses:house2, person:james";
var obj = str.split(',').reduce( (a,b) => { // split the string on comma
var parts = b.trim().split(':'); // split the parts on colon
if (! (parts[0] in a) ) a[parts[0]] = []; // if no key, create an array
a[parts[0]].push(parts[1]); // push the value to the array
return a; // return the object for next iteration
}, {})
console.log(obj)
答案 3 :(得分:0)
RegExp
方法。
var string = "books:book1, books:book2, houses:house1, houses:house2, person:james",
arr = string.split(', '), obj = {}, res = [];
arr.forEach(function(v){
var array = [],
elem1 = v.match(/\w+(?=:)/),
elem2 = v.match(/(?:)\w+/g)[1];
if (!obj[elem1]){
obj[elem1] = [];
obj[elem1].push(elem2);
} else {
obj[elem1].push(elem2);
}
});
console.log(obj);
答案 4 :(得分:0)
这样的事情:
var string = "books:book1, books:book2, houses:house1, houses:house2, person:james";
var books = [];
var houses = [];
var person = [];
string.split(", ").forEach(function(e) {
switch (e.split(":")[0]) {
case "books":
books.push(e.split(":")[1]);
break;
case "houses":
houses.push(e.split(":")[1]);
break;
case "person":
person.push(e.split(":")[1]);
break;
}
});
代码如下:
books
,houses
和person
制作三个新数组。,
分隔。:
books
,houses
或person
,则在右侧上附加字符串分别是books
,houses
或person
数组。答案 5 :(得分:0)
这将为你利用window
创建全局变量,但是对于上帝的爱,请不要 - 只使用一个对象。虽然这个答案是你要求的,但其他答案是你真正想要的。
var str = "books:book1, books:book2, houses:house1, houses:house2, person:james";
var splitStr = str.split(',');
var i;
var key;
var val;
var temp;
for (i=0; i<splitStr.length; i++) {
temp = splitStr[i].split(':');
key = temp[0].replace(/ /g,'');
val = temp[1];
if (!window[key]) {
window[key] = [];
}
window[key].push(val);
}
console.log(books, houses, person);
&#13;
答案 6 :(得分:0)
首先,您可以使用split
字符串方法两次来分解数据,然后将它们变成(几乎)全局变量,您可以将它们设置为窗口属性,但是如果这些值可能会很危险通过干扰现有的财产。
var pairs = str.split(", ");
for(var i = 0; i < pairs.length; i++) {
var key = pairs[i].split(":")[0];
var val = pairs[i].split(":")[1];
if(window.hasOwnProperty(key)) {
window[key].push(val);
}
else {
window[key] = [];
window[key].push(val);
}
}
建议不要在字符串的来源未知的情况下使用此方法,因为它会直接修改窗口属性,如果被覆盖,其中一些属性可能会导致意外行为。