我需要从这个对象中构造并获取title,child,childTitle的值
const obj1 = {
title : 'foo',
child : {
title2 : 'bar'
}
}
let {title, child} = obj1;
console.log(title) //'foo'
console.log(child) //{ title : 'bar' }
// but couldn't get child object this way
let { title , child : { title2 } } = obj1;
console.log(title) //'foo'
console.log(child) //unDefined
console.log(title2) //'bar'
Run Code Online (Sandbox Code Playgroud)
我怎么能得到孩子的对象?
如何编写包含两个搜索词的查询,这些搜索词与突出显示内部匹配的嵌套对象相匹配。
\n以下是示例用例:
\n我有一个映射:
\n"mappings": {\n "properties": {\n "grocery_name": {\n "type": "text"\n },\n "items": {\n "type": "nested",\n "properties": {\n "name": {\n "type": "text"\n },\n "stock": {\n "type": "integer"\n },\n "category": {\n "type": "text"\n }\n }\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n数据如下所示
\n{\n "grocery_name": "Elastic Eats",\n "items": [\n {\n "name": "Red banana",\n "stock": "12",\n "category": "fruit"\n },\n {\n "name": "Cavendish banana",\n "stock": "10",\n "category": "fruit"\n },\n {\n "name": "peach",\n "stock": "10",\n "category": "fruit"\n },\n {\n "name": "carrot",\n "stock": "9",\n …Run Code Online (Sandbox Code Playgroud) 我有一个从文件中返回的 JSON 对象./jsonData.json。
在此文件中,我有以下数据:
注意:这是从文件加载的整个 JSON 数据。
import QuizData from './quizData.json'
Run Code Online (Sandbox Code Playgroud)
这是一个新应用程序,QuizData以下所有内容也是如此:
[
{
"id": 1,
"name": "Lesson 1",
"topics": [
{
"topicID": 1,
"topicName": "Science",
"topicDescription": "Science quiz questions"
},
{
"topicID": 2,
"topicName": "General Knowledge",
"topicDescription": "General Knowledge Quiz Questions"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试为找到的每个主题获取主题名称并将其作为文本发布。
这是我的代码:
<FlatList
data={QuizData}
renderItem={({ item, index }) =>
<View>
<Text>{item.topics.topicName}</Text>
</View>
}
keyExtractor={(item) => item.topicID.toString()}
/>
Run Code Online (Sandbox Code Playgroud)
我也试过:
{item.topics.[index].topicName}
Run Code Online (Sandbox Code Playgroud)
和
{item.topics[index][topicName]}
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
undefined 不是一个对象。
然后我认为它可能需要:
data={QuizData.topics}
Run Code Online (Sandbox Code Playgroud)
然后将 renderItem 更改为:
{item.topicName} …Run Code Online (Sandbox Code Playgroud) 我正在尝试遍历以下嵌套对象并获得如下输出:
const preference = {
"ethnicity": {
"value": "Gurung",
"rank": 1
},
"occupation": {
"value": "Banker",
"rank": 2
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下操作:
let preferenceRank = {};
preference.map(pref => {
preferenceRank[pref.rank] = pref;
});
console.log(preferenceRank);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
"TypeError: preference.map is not a function"...
Run Code Online (Sandbox Code Playgroud)
所需输出:
{
1: "ethnicity",
2: "occupation",
}
Run Code Online (Sandbox Code Playgroud)