鍍金池/ 問(wèn)答/Java  HTML/ jq,json對(duì)象拼接

jq,json對(duì)象拼接

var a = {'type': 1, list: [{'id': 1, 'name': aa},{'id': 2, 'name': bb}]};
var b = {'type': 2, list: [{'id': 3, 'name': cc},{'id': 4, 'name': bb}]};
結(jié)果輸出
var c = [{'type': 1, list: [{'id': 1, 'name': aa},{'id': 2, 'name': bb}]},{'type': 2, list: [{'id': 3, 'name': cc},{'id': 4, 'name': bb}]}]
這個(gè)結(jié)果直接 var c = [a,b]嗎?

回答
編輯回答
小眼睛

@Meathill

    can2 = JSON.stringify(can2);
    can3 = JSON.stringify(can3);
    can4 = JSON.stringify(can4);
    layout_str = can2.substring(0, can2.length - 1) + ',' + can3.substring(1, can3.length - 1)+','+ can4.substring(1, can4.length);
   得到了想要的結(jié)果
   [{"type":2,"enabled":1,"channel_list":[{"channel_id":"","template":"15"},{"channel_id":"","template":"17"},{"channel_id":"","template":"14"},{"channel_id":"","template":"16"},{"channel_id":"","template":"14"}]},{"type":2,"enabled":1,"channel_list":[{"channel_id":"","template":"15"},{"channel_id":"","template":"17"},{"channel_id":"","template":"14"},{"channel_id":"","template":"16"},{"channel_id":"","template":"14"}]},{"type":3,"enabled":1,"channel_list":[{"channel_id":"1091991"},{"channel_id":"1091993"}]}]
   但是剛開(kāi)始
   can2 = JSON.stringify(can2);
    can3 = JSON.stringify(can3);
    can4 = JSON.stringify(can4);
    console.log(can2+','+ can3+','+ can4);
    結(jié)果
    [{"type":2,"enabled":1,"channel_list":[{"channel_id":"","template":"15"},{"channel_id":"","template":"17"},{"channel_id":"","template":"14"},{"channel_id":"","template":"16"},{"channel_id":"","template":"14"}]}],[{"type":2,"enabled":1,"channel_list":[{"channel_id":"","template":"15"},{"channel_id":"","template":"17"},{"channel_id":"","template":"14"},{"channel_id":"","template":"16"},{"channel_id":"","template":"14"}]}],[{"type":3,"enabled":1,"channel_list":[{"channel_id":"1091991"},{"channel_id":"1091993"}]}]
    沒(méi)想到好辦法,然后就用截取字符串的辦法了
2017年11月28日 19:40
編輯回答
懶豬

方法、函數(shù)都在下面,深復(fù)制/淺復(fù)制隨便選,函數(shù)用 ES6 寫(xiě)的

var aa = 1,
    bb = 2,
    cc = 3,
    dd = 4;
var a = { 'type': 1, list: [{ 'id': 1, 'name': aa }, { 'id': 2, 'name': bb }] };
var b = { 'type': 2, list: [{ 'id': 3, 'name': cc }, { 'id': 4, 'name': bb }] };
var c = [{ 'type': 1, list: [{ 'id': 1, 'name': aa }, { 'id': 2, 'name': bb }] }, { 'type': 2, list: [{ 'id': 3, 'name': cc }, { 'id': 4, 'name': bb }] }]

var resultDeep = [JSON.parse(JSON.stringify(a)), JSON.parse(JSON.stringify(b))];
var resultShallow = [a, b];

console.log(JSON.stringify(resultShallow) === JSON.stringify(c));//true
console.log(JSON.stringify(resultDeep) === JSON.stringify(c));//true

//函數(shù)提煉
function splicingObjects(deepOrShallow, ...obj) {
    let result = [];
    if (deepOrShallow === 'deep') {
        obj.forEach((obj) => { result.push(JSON.parse(JSON.stringify(obj)));});
    } else if (deepOrShallow === 'shallow') {
        result = [...obj];
    }
    return result;
}

let resultDeep2 = splicingObjects('deep',a,b);
let resultShallow2=splicingObjects('shallow',a,b);

console.log(JSON.stringify(resultShallow2) === JSON.stringify(c));//true
console.log(JSON.stringify(resultDeep2) === JSON.stringify(c));//true

2018年9月15日 17:54
編輯回答
孤客

題主在說(shuō)什么?這不就是 c=[a,b] 么?

2018年1月10日 10:00