jq,json对象拼接

小丑的帽子
  • 101

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 c = [a,b]吗?

回复
阅读 2.8k
3 个回答

题主在说什么?这不就是 c=[a,b] 么?

方法、函数都在下面,深复制/浅复制随便选,函数用 ES6 写的

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

//函数提炼
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

@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);
   得到了想要的结果
   [{"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"}]}]
   但是刚开始
   can2 = JSON.stringify(can2);
    can3 = JSON.stringify(can3);
    can4 = JSON.stringify(can4);
    console.log(can2+','+ can3+','+ can4);
    结果
    [{"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"}]}]
    没想到好办法,然后就用截取字符串的办法了
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏