我有一组数据,我想建立一个带有自定义属性名称的新数据,现有的数据集已嵌套,这也是我需要将其规范化以适应我自己的原因之一使用
说我有这套现有数据
[{
name:"abc",
company:{name:"abc company",age_of_company:1},
age: 19
}]
我想构建一个新的对象数组,其中属性和嵌套级别将是这样的
[{
ceo_name:"abc",
company_name:"abc company",
age: 19
}]
我被困了
const newData = existingData.map(obj => (
//obj.company.name I don't know what to do here
)
答案 0 :(得分:1)
您可以按如下方式对传递给map
的函数的参数进行解构:
existingData.map(
({age, name: ceo_name, company: {name: company_name}}) =>
({ceo_name, company_name, age}))
答案 1 :(得分:1)
const newData = existingData.map(obj => (
{
ceo_name: obj.name,
company_name: obj.company.name,
age: obj.age
}
))
答案 2 :(得分:0)
试试这个
[{
name:"abc",
company:{name:"abc company",age_of_company:1},
age: 19
}].map(element => {
return {
ceo_name: element.name,
company_name: element.company.name,
age: element.age
}
})
答案 3 :(得分:0)
// ...
$collection = collect($collection);
$page = 1;
$perPage = 10;
$paginate = new LengthAwarePaginator($collection->forPage($page, $perPage), $collection->count(), $perPage, $page, ['path'=>url('api/products')]);