从现有 API 对象创建新数组对象
Creating new array object from existing API object
我正在尝试过滤掉数组中的“国家/地区”键值。
下面是 JSON 结构。
let apidata = [
{
"Brand":"Puma",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Country": [
{
"City": "Mumbai",
"Location": "Andheri",
},{
"City": "Bangalore",
"Location": "MG Road",
},
],
"Shipping": [
{
"pincode":"112144",
"shipping":"yes",
},{
"pincode":"111451",
"shipping":"No",
},
]
},{
"Brand":"adidas",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321xv",
"Country": [
{
"City": "Chennai",
"Location": "Marina",
},{
"City": "Gurgaon",
"Location": "Huda City",
},
],
"Shipping": [
{
"pincode":"114144",
"shipping":"yes",
},{
"pincode":"111414",
"shipping":"No",
},
]
},
]
预期的输出应该是:
b = [
{
"City": "Mumbai",
"Location": "Andheri",
"uid": 0
},
{
"City": "Bangalore",
"Location": "MG Road",
"uid": 1
},
{
"City": "Chennai",
"Location": "Marina",
"uid": 0
},
{
"City": "Gurgaon",
"Location": "Huda City",
"uid": 1
}
]
下面是我试图实现的代码
let b = [];
apidata.map((ele) => {
return Object.keys(ele).filter(key => key === "Country").reduce((obj, ele1, index) => {
// debugger;
if(b.length == 0)
b = ele[ele1].map((i,index)=>({...i,"uid":index}));
else
b = [...b, ...ele[ele1].map((i,index)=>({...i,"uid":index}))] ;
return b
},{})
})
我使用过滤器概念过滤掉键并结合reduce创建一个新对象。但是我无法获得所需的输出。
谁能帮我看看我哪里出错了?
当您只需要一个 属性 时,无需过滤密钥。只需直接访问 属性。
使用 flatMap()
将每个 Country
属性 的映射结果合并到一个数组中。
let apidata = [{
"Brand": "Puma",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Country": [{
"City": "Mumbai",
"Location": "Andheri",
}, {
"City": "Bangalore",
"Location": "MG Road",
}, ],
"Shipping": [{
"pincode": "112144",
"shipping": "yes",
}, {
"pincode": "111451",
"shipping": "No",
}, ]
}, {
"Brand": "adidas",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321xv",
"Country": [{
"City": "Chennai",
"Location": "Marina",
}, {
"City": "Gurgaon",
"Location": "Huda City",
}, ],
"Shipping": [{
"pincode": "114144",
"shipping": "yes",
}, {
"pincode": "111414",
"shipping": "No",
}, ]
}, ];
let result = apidata.flatMap(({Country}) => Country.map((city, i) => ({...city, uid: i})));
console.log(result);
我正在尝试过滤掉数组中的“国家/地区”键值。 下面是 JSON 结构。
let apidata = [
{
"Brand":"Puma",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Country": [
{
"City": "Mumbai",
"Location": "Andheri",
},{
"City": "Bangalore",
"Location": "MG Road",
},
],
"Shipping": [
{
"pincode":"112144",
"shipping":"yes",
},{
"pincode":"111451",
"shipping":"No",
},
]
},{
"Brand":"adidas",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321xv",
"Country": [
{
"City": "Chennai",
"Location": "Marina",
},{
"City": "Gurgaon",
"Location": "Huda City",
},
],
"Shipping": [
{
"pincode":"114144",
"shipping":"yes",
},{
"pincode":"111414",
"shipping":"No",
},
]
},
]
预期的输出应该是:
b = [
{
"City": "Mumbai",
"Location": "Andheri",
"uid": 0
},
{
"City": "Bangalore",
"Location": "MG Road",
"uid": 1
},
{
"City": "Chennai",
"Location": "Marina",
"uid": 0
},
{
"City": "Gurgaon",
"Location": "Huda City",
"uid": 1
}
]
下面是我试图实现的代码
let b = [];
apidata.map((ele) => {
return Object.keys(ele).filter(key => key === "Country").reduce((obj, ele1, index) => {
// debugger;
if(b.length == 0)
b = ele[ele1].map((i,index)=>({...i,"uid":index}));
else
b = [...b, ...ele[ele1].map((i,index)=>({...i,"uid":index}))] ;
return b
},{})
})
我使用过滤器概念过滤掉键并结合reduce创建一个新对象。但是我无法获得所需的输出。
谁能帮我看看我哪里出错了?
当您只需要一个 属性 时,无需过滤密钥。只需直接访问 属性。
使用 flatMap()
将每个 Country
属性 的映射结果合并到一个数组中。
let apidata = [{
"Brand": "Puma",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Country": [{
"City": "Mumbai",
"Location": "Andheri",
}, {
"City": "Bangalore",
"Location": "MG Road",
}, ],
"Shipping": [{
"pincode": "112144",
"shipping": "yes",
}, {
"pincode": "111451",
"shipping": "No",
}, ]
}, {
"Brand": "adidas",
"id": "c5d58bef-f1c2-4b7c-a6d7-f64df12321xv",
"Country": [{
"City": "Chennai",
"Location": "Marina",
}, {
"City": "Gurgaon",
"Location": "Huda City",
}, ],
"Shipping": [{
"pincode": "114144",
"shipping": "yes",
}, {
"pincode": "111414",
"shipping": "No",
}, ]
}, ];
let result = apidata.flatMap(({Country}) => Country.map((city, i) => ({...city, uid: i})));
console.log(result);