[英]assigning values to a computed property vuejs
I have this array of objects:我有这个对象数组:
columnData=[
{Address: "1 fake st"},
{Application: "bts sport"},
{Description: "good"},
{Issue Date: "2012-08-09"},
{Status: "active"},
{Unit: null}
]
And then I want to assign some of columnData
elements to a new array of objects using a computed property and the new array should look like:然后我想使用计算属性将一些
columnData
元素分配给一个新的对象数组,新数组应该如下所示:
newData= [
{
label: "Address",
value: "1 fake st"
},
{
label: "Descripttion",
value: "good"
},
{
label: "Unit",
value: null
}
]
I have tried this piece of code but it doesn't work:我已经尝试了这段代码,但它不起作用:
computed:{
newData() {
return [
{
label: Object.keys(this.columnData[0]),
value: Object.values(this.columnData[0])
},
{
label: Object.keys(this.columnData[2]),
value: Object.values(this.columnData[2])
},
{
label: Object.keys(this.columnData[5]),
value: Object.values(this.columnData[5])
}
];
}
}
How can I create the newData
array the way I want it to look like using a computed property?如何使用计算属性按照我希望的方式创建
newData
数组?
You need Object.keys(obj)[0]
and Object.values(obj)[0]
, respectively to get the first property name and value of each object in the array.您需要
Object.keys(obj)[0]
和Object.values(obj)[0]
分别获取数组中每个 ZA8CFDE6331BD59EB2AC96F8911C4B6 的第一个属性名称和值。
const columnData=[ {Address: "1 fake st"}, {Application: "bts sport"}, {Description: "good"}, {"Issue Date": "2012-08-09"}, {Status: "active"}, {Unit: null} ]; const newData = columnData.slice(0,2).concat(columnData[5]).map(obj=>({ label: Object.keys(obj)[0], value: Object.values(obj)[0] })); console.log(newData);
You can also use Object.entries
to get both the property name and value in one array.您还可以使用
Object.entries
在一个数组中获取属性名称和值。
const columnData=[ {Address: "1 fake st"}, {Application: "bts sport"}, {Description: "good"}, {"Issue Date": "2012-08-09"}, {Status: "active"}, {Unit: null} ]; const newData = columnData.slice(0,2).concat(columnData[5]).map(obj=>{ const [label, value] = Object.entries(obj)[0]; return {label, value}; }); console.log(newData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.