合并两个不同json数组中的相同特定json对象

时间:2022-07-26 12:20:06

I would like to merge the same specific json object on two different json arrays dependent on an json data ID.

我想在依赖于json数据ID的两个不同的json数组上合并相同的特定json对象。

JSON Data Set 1

JSON数据集1

{
    "Product":[
                {
                "product_id": "123",
                "location_id": "222",
                "product_code": "abc",
                },
                {
                "product_id": "456",
                "location_id": "111",
                "product_code": "xyz",
                }
            ]
}

JSON Data set 2

JSON数据集2

{
    "Location":[

            {
                "location_id": 111,
                "location_name": "alpha"
            },
            {
                "location_id": 222,
                "location_name": "tango"
            }


        ]
}

The Results would be something like this

结果将是这样的

{
    "Product":[

                {
                    "product_id": "456",
                    "location_id": "111",
                    "product_code": "xyz",
                    "location_name": "alpha"
                },
                {
                    "product_id": "123",
                    "location_id": "222",
                    "product_code": "abc",
                    "location_name": "tango"
                }
            ]
}

So far this is the code I've done.

到目前为止,这是我所做的代码。

var finalJson = {};

                    _.each(_.keys(productArray,locationArray), function(key) {
                        finalJson[key] = _.flatten(_.zip(productArray[key], locationArray[key]));
                    });

                    console.log(finalJson);

2 个解决方案

#1


2  

A simple algorithm could be using nested loops to go through both arrays, like this:

一个简单的算法可能是使用嵌套循环来遍历两个数组,如下所示:

let allProducts = [{
    "product_id": "123",
    "location_id": "222",
    "product_code": "abc",
  },
  {
    "product_id": "456",
    "location_id": "111",
    "product_code": "xyz",
  }
];


let allLocations = [

  {
    "location_id": 111,
    "location_name": "alpha"
  },
  {
    "location_id": 222,
    "location_name": "tango"
  }
];

let result = allProducts.map((product) => {
  let matchingLocation = allLocations.find((location) => {
    return location.location_id == product.location_id;
  });
  return Object.assign(product, matchingLocation);
})

console.log(result);

#2


1  

Make a hashmap of the locations using location id as keys, then iterate the Product array

使用location id作为键来创建位置的散列图,然后迭代Product数组

let arr1 = {
  "Product": [{
    "product_id": "123",
    "location_id": "222",
    "product_code": "abc",
  }, {
    "product_id": "456",
    "location_id": "111",
    "product_code": "xyz",
  }]
}

let arr2 = {
  "Location": [
    {
      "location_id": 111,
      "location_name": "alpha"
    }, {
      "location_id": 222,
      "location_name": "tango"
    }
  ]
}
let locIds= _.keyBy(arr2.Location, 'location_id');
_.each(arr1.Product, (o) => _.assign(o, locIds[o.location_id]));

console.log(arr1.Product)
.as-console-wrapper{max-height:100%!important}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

#1


2  

A simple algorithm could be using nested loops to go through both arrays, like this:

一个简单的算法可能是使用嵌套循环来遍历两个数组,如下所示:

let allProducts = [{
    "product_id": "123",
    "location_id": "222",
    "product_code": "abc",
  },
  {
    "product_id": "456",
    "location_id": "111",
    "product_code": "xyz",
  }
];


let allLocations = [

  {
    "location_id": 111,
    "location_name": "alpha"
  },
  {
    "location_id": 222,
    "location_name": "tango"
  }
];

let result = allProducts.map((product) => {
  let matchingLocation = allLocations.find((location) => {
    return location.location_id == product.location_id;
  });
  return Object.assign(product, matchingLocation);
})

console.log(result);

#2


1  

Make a hashmap of the locations using location id as keys, then iterate the Product array

使用location id作为键来创建位置的散列图,然后迭代Product数组

let arr1 = {
  "Product": [{
    "product_id": "123",
    "location_id": "222",
    "product_code": "abc",
  }, {
    "product_id": "456",
    "location_id": "111",
    "product_code": "xyz",
  }]
}

let arr2 = {
  "Location": [
    {
      "location_id": 111,
      "location_name": "alpha"
    }, {
      "location_id": 222,
      "location_name": "tango"
    }
  ]
}
let locIds= _.keyBy(arr2.Location, 'location_id');
_.each(arr1.Product, (o) => _.assign(o, locIds[o.location_id]));

console.log(arr1.Product)
.as-console-wrapper{max-height:100%!important}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>