展平嵌套 JSON 对象
Flatten nested JSON object
我有一个这样的 json 对象:
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
我想要这样的输出:即对于每个城市,我想展平数组。
[
['My Name', 'London', 'UK'],
['My Name','Mumbai', 'IN'],
['My Name','New York', 'US']
]
我试过展平等,但不知道如何实现。
有人可以帮我吗?
谢谢,
萨西
这应该可以解决问题![=11=]
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
function flattenIt(obj) {
const name = obj.name;
const cityData = obj.cities;
return cityData.map(({city, country}) => [name, city, country])
}
console.log(flattenIt(people));
如果你想要JSON输出:
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
let result = people.cities.map(data => {
return {
name: people.name,
city: data.city,
country: data.country
}
})
console.log(result)
为多人扩展此功能:
const peopleExtended = [{
name: 'My Name',
cities: [{
city: 'London',
country: 'UK'
}, {
city: 'Mumbai',
country: 'IN'
}, {
city: 'New York',
country: 'US'
}],
}, {
name: 'Person 2',
cities: [{
city: 'Birmingham',
country: 'UK'
}, {
city: 'Delhi',
country: 'IN'
}, {
city: 'New York',
country: 'US'
}],
}]
function flattenCities(person) {
return person.cities.map(data => {
return {
name: person.name,
city: data.city,
country: data.country
}
})
}
peopleExtended.forEach(person => {
let flatArray = flattenCities(person);
console.log(person.name, flatArray)
})
我有一个这样的 json 对象:
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
我想要这样的输出:即对于每个城市,我想展平数组。
[
['My Name', 'London', 'UK'],
['My Name','Mumbai', 'IN'],
['My Name','New York', 'US']
]
我试过展平等,但不知道如何实现。 有人可以帮我吗? 谢谢, 萨西
这应该可以解决问题![=11=]
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
function flattenIt(obj) {
const name = obj.name;
const cityData = obj.cities;
return cityData.map(({city, country}) => [name, city, country])
}
console.log(flattenIt(people));
如果你想要JSON输出:
const people = {
name: 'My Name',
cities: [{city: 'London', country: 'UK'},{city: 'Mumbai', country: 'IN'},{city: 'New York', country: 'US'}],
}
let result = people.cities.map(data => {
return {
name: people.name,
city: data.city,
country: data.country
}
})
console.log(result)
为多人扩展此功能:
const peopleExtended = [{
name: 'My Name',
cities: [{
city: 'London',
country: 'UK'
}, {
city: 'Mumbai',
country: 'IN'
}, {
city: 'New York',
country: 'US'
}],
}, {
name: 'Person 2',
cities: [{
city: 'Birmingham',
country: 'UK'
}, {
city: 'Delhi',
country: 'IN'
}, {
city: 'New York',
country: 'US'
}],
}]
function flattenCities(person) {
return person.cities.map(data => {
return {
name: person.name,
city: data.city,
country: data.country
}
})
}
peopleExtended.forEach(person => {
let flatArray = flattenCities(person);
console.log(person.name, flatArray)
})