我有一个问题: 我有这个数据库:
-----------------------------------------------------------------
| Germany | Italy | Belgium |
-----------------------------------------------------------------
| Gerhard Richter | Leonardo da Vinci | Anton van Dyck |
-----------------------------------------------------------------
| Paul Klee | Sandro Botticelli | Michaël Borremans |
-----------------------------------------------------------------
| Neo Rauch | Masaccio | Paul Delvaux |
-----------------------------------------------------------------
我想在一行中为每一行而不是每一列添加一个值:
此处图片:https://puu.sh/B81Po/03395bad74.png
我添加了.split(", ");
,但现在将每一行分为3行,这很好,但是我也需要添加其他列值。
代码:
const Obj = {
"0":"Neo Rauch, Paul Klee, Gerhard Richter",
"1":"Masaccio, Sandro Botticelli, Leonardo da Vinci",
"2":"Paul Delvaux, Michaël Borremans, Anton van Dyck"
};
const Obj2 = {}
Object.keys(Obj).forEach(function(key) {
Obj2[key] = Obj[key].split(", ");
});
console.log(Obj2);
编辑: 现在怎么样:
"0":"Neo Rauch, Paul Klee, Gerhard Richter",
"1":"Masaccio, Sandro Botticelli, Leonardo da Vinci",
"2":"Paul Delvaux, Michaël Borremans, Anton van Dyck"
我想要什么:
"0":"Neo Rauch", "Masaccio", "Paul Delvaux"
"1":"Paul Klee", "Sandro Botticelli", "Michaël Borremans"
"2":"Gerhard Richter", "Leonardo da Vinci", "Anton van Dyck"
答案 0 :(得分:0)
您可以使用“,”将数组连接起来,以便在每个单词周围加上双引号。希望这就是你想要的。
const Obj = {
"0":"Neo Rauch, Paul Klee, Gerhard Richter",
"1":"Masaccio, Sandro Botticelli, Leonardo da Vinci",
"2":"Paul Delvaux, Michaël Borremans, Anton van Dyck"
};
const Obj2 = {}
Object.keys(Obj).forEach(function(key) {
Obj2[key] = Obj[key].split(", ").join('","');
});
console.log(Obj2);
答案 1 :(得分:0)
您想要的不是格式正确的对象,这将导致语法错误。这是一种获得我认为是您想要的结果的方法。
第一个控制台是嵌套对象,第二个是具有嵌套数组的对象。
const Obj = {
"0":"Neo Rauch, Paul Klee, Gerhard Richter",
"1":"Masaccio, Sandro Botticelli, Leonardo da Vinci",
"2":"Paul Delvaux, Michaël Borremans, Anton van Dyck"
};
const Obj2 = {};
const Obj3 = {};
Object.keys(Obj).forEach(function(key) {
var string = [];
Object.keys(Obj).forEach(function(k) {
string.push(Obj[k].split(", ")[key]);
});
var obj = string.reduce(function(acc, cur, i) {
acc[i] = cur;
return acc;
}, {});
Obj2[key] = obj;
Obj3[key] = string;
});
console.log(Obj2);
console.log(Obj3);