[英]Combine two nested json objects with javascript and loop through it with a key
I have a few json objects (obj1,obj2...) and each is taken from.txt files:我有几个 json 个对象(obj1、obj2...),每个对象都取自 .txt 文件:
{
"countries": [
{
"Country name": "China",
"Flag": "CN",
"Population": 1395380000,
"undefined": "#688144"
}, ... ]}
and和
{
"countries": [
{
"Country name": "India",
"Flag": "IN",
"Population": 1338677000,
"undefined": "#B78A31"
}, ...]}
And so on.等等。 Now I want to combine them like this:现在我想像这样组合它们:
{
"countries": [
{
"Country name": "China",
"Flag": "CN",
"Population": 1395380000,
"undefined": "#688144"
},
{
"Country name": "India",
"Flag": "IN",
"Population": 1338677000,
"undefined": "#B78A31"
},
... ]}
So I can loop through data like this:所以我可以像这样遍历数据:
let obj1 = {}; //saved Data From Txt1;
let obj2 = {}; //saved Data From Txt2
...
let obj = combined?
for (var key in obj.countries) {
var num1 = obj.countries[key].Population+popholder;
if (target >= popholder && target <= num1) {
var country = obj.countries[key]['Country name'];
var testas = document.getElementById("countryname")
}}
How could I achieve this?我怎么能做到这一点?
Just concat the arrays:只需连接 arrays:
const obj1 = { "countries": [{ "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" } ] }; const obj2 = { "countries": [{ "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" } ] }; const result = { countries: [...obj1.countries, ...obj2.countries] }; console.log(result);
You can use create a new object with the same countries
properties and use Array#concat
to combine all the arrays of countries into one:您可以使用创建具有相同countries
地区属性的新 object 并使用Array#concat
将所有国家/地区的 arrays 合并为一个:
const obj1 = { "countries": [{ "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" }, /*... */ ] }; const obj2 = { "countries": [{ "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" }, /*... */ ] }; const obj3 = { "countries": [{ "Country name": "Sealand", "Flag": "", "Population": 27, "undefined": "#0000FF" }, /*... */ ] }; const combined = { countries: [].concat( obj1.countries, obj2.countries, obj3.countries ) }; console.log(combined);
If you have an array of the objects, you can combine Array#map
with spread syntax to extract the countries
arrays and combine them into one:如果你有一个对象数组,你可以结合Array#map
和扩展语法来提取countries
arrays 并将它们组合成一个:
const obj1 = { "countries": [{ "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" }, /*... */ ]}; const obj2 = { "countries": [{ "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" }, /*... */ ]}; const obj3 = { "countries": [{ "Country name": "Sealand", "Flag": "", "Population": 27, "undefined": "#0000FF" }, /*... */ ]}; const objArr = [obj1, obj2, obj3]; const combined = { combined: [].concat(...objArr.map(x => x.countries) ) }; console.log(combined)
You can push
all of the arrays into one.您可以push
所有 arrays 合二为一。
const obj1 = { "countries": [ { "Country name": "China", "Flag": "CN", "Population": 1395380000, "undefined": "#688144" }, ]} const obj2 = { "countries": [ { "Country name": "India", "Flag": "IN", "Population": 1338677000, "undefined": "#B78A31" },]}; const res = [obj1, obj2, /*...*/].reduce((acc,{countries})=>(acc.countries.push(...countries),acc), {countries: []}); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.