带有对象的对象的名称数组
Array of names from object with objects
我有包含对象的对象,如下所示:
{
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
}
现在我需要获取名称数组 ["Andorra", "United Arab Emirates", "Afghanistan"]
以便我可以将这些值以这种格式发送到 select 字段:
<q-select color="teal" outlined v-model="model" :options="options" label="Label">
<template v-slot:append>
</template>
</q-select>
<script>
export default {
data () {
return {
model: null,
options: [
'Google', 'Facebook', 'Twitter', 'Apple', 'Oracle'
]
}
}
}
</script>
我尝试将对象转换为数组,但当我尝试使用 array.name
访问时,这不起作用。我只需要从这个对象的对象中获取名称。
有谁知道我该怎么做?
您可以获得值和名称 属性。
var object = { AD: { name: "Andorra", native: "Andorra", phone: "376", continent: "EU", capital: "Andorra la Vella", currency: "EUR", languages: ["ca"] }, AE: { name: "United Arab Emirates", native: "دولة الإمارات العربية المتحدة", phone: "971", continent: "AS", capital: "Abu Dhabi", currency: "AED", languages: ["ar"] }, AF: { name: "Afghanistan", native: "افغانستان", phone: "93", continent: "AS", capital: "Kabul", currency: "AFN", languages: ["ps", "uz", "tk"] } },
arrayOfNames = Object.values(object).map(({ name }) => name);
console.log(arrayOfNames);
使用Object.keys()
and Array.prototype.map()
填充新数组:
const obj = {
"AD": {
"name": "Andorra",
"native": "Andorra", //...
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة", //...
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان", //...
}
};
const namesOptions = Object.keys(obj).map(k => obj[k].name);
console.log(namesOptions)
你可以这样做
var obj = {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
};
var values = Object.keys(obj).map(value=> obj[value].name);
console.log(values);
也是一种简单的方法
const test = {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
}
const countries = []
for (obj in test) {
countries.push(test[obj].name)
}
console.log(countries)
您可以通过减少键和映射名称将对象减少到数组中。
Object.keys(data).reduce((a, k) => [...a, data[k].name], [])
let data = getData();
console.log(Object.keys(data).reduce((a, k) => [...a, data[k].name], []));
function getData() {
return {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [ "ca" ]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [ "ar" ]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [ "ps", "uz", "tk" ]
}
};
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
我有包含对象的对象,如下所示:
{
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
}
现在我需要获取名称数组 ["Andorra", "United Arab Emirates", "Afghanistan"]
以便我可以将这些值以这种格式发送到 select 字段:
<q-select color="teal" outlined v-model="model" :options="options" label="Label">
<template v-slot:append>
</template>
</q-select>
<script>
export default {
data () {
return {
model: null,
options: [
'Google', 'Facebook', 'Twitter', 'Apple', 'Oracle'
]
}
}
}
</script>
我尝试将对象转换为数组,但当我尝试使用 array.name
访问时,这不起作用。我只需要从这个对象的对象中获取名称。
有谁知道我该怎么做?
您可以获得值和名称 属性。
var object = { AD: { name: "Andorra", native: "Andorra", phone: "376", continent: "EU", capital: "Andorra la Vella", currency: "EUR", languages: ["ca"] }, AE: { name: "United Arab Emirates", native: "دولة الإمارات العربية المتحدة", phone: "971", continent: "AS", capital: "Abu Dhabi", currency: "AED", languages: ["ar"] }, AF: { name: "Afghanistan", native: "افغانستان", phone: "93", continent: "AS", capital: "Kabul", currency: "AFN", languages: ["ps", "uz", "tk"] } },
arrayOfNames = Object.values(object).map(({ name }) => name);
console.log(arrayOfNames);
使用Object.keys()
and Array.prototype.map()
填充新数组:
const obj = {
"AD": {
"name": "Andorra",
"native": "Andorra", //...
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة", //...
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان", //...
}
};
const namesOptions = Object.keys(obj).map(k => obj[k].name);
console.log(namesOptions)
你可以这样做
var obj = {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
};
var values = Object.keys(obj).map(value=> obj[value].name);
console.log(values);
也是一种简单的方法
const test = {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [
"ca"
]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [
"ar"
]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [
"ps",
"uz",
"tk"
]
}
}
const countries = []
for (obj in test) {
countries.push(test[obj].name)
}
console.log(countries)
您可以通过减少键和映射名称将对象减少到数组中。
Object.keys(data).reduce((a, k) => [...a, data[k].name], [])
let data = getData();
console.log(Object.keys(data).reduce((a, k) => [...a, data[k].name], []));
function getData() {
return {
"AD": {
"name": "Andorra",
"native": "Andorra",
"phone": "376",
"continent": "EU",
"capital": "Andorra la Vella",
"currency": "EUR",
"languages": [ "ca" ]
},
"AE": {
"name": "United Arab Emirates",
"native": "دولة الإمارات العربية المتحدة",
"phone": "971",
"continent": "AS",
"capital": "Abu Dhabi",
"currency": "AED",
"languages": [ "ar" ]
},
"AF": {
"name": "Afghanistan",
"native": "افغانستان",
"phone": "93",
"continent": "AS",
"capital": "Kabul",
"currency": "AFN",
"languages": [ "ps", "uz", "tk" ]
}
};
}
.as-console-wrapper { top: 0; max-height: 100% !important; }