我如何从 javascript 中的对象中检索特定字段?
how can i retrieve specific fields from object in javascript?
我想检索 JavaScript 对象的一部分。
最终对象应具有所有 hits.hits._source
我用 lodash 或下划线尝试了不同的东西,但我没有得到它
我的初始对象是
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "users",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"id": "112",
"type": "pro",
"email": "liz@google.com"
}
},
{
"_index": "users",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"type": "pro",
"email": "john@google.com"
}
}
]
}
}
我想得到
[
{
"id": "112",
"type": "pro",
"email": "liz@google.com"
},
{
"id": "2",
"type": "pro",
"email": "john@google.com"
}
]
检查此 fiddle:http://jsfiddle.net/7jLz13n6/
你可以这样做:
var output=[];
var input=o.hits.hits;
for(i=0;i<input.length;i++)
output.push(input[i]._source);
console.log(output);
我想检索 JavaScript 对象的一部分。
最终对象应具有所有 hits.hits._source 我用 lodash 或下划线尝试了不同的东西,但我没有得到它
我的初始对象是
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "users",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"id": "112",
"type": "pro",
"email": "liz@google.com"
}
},
{
"_index": "users",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"type": "pro",
"email": "john@google.com"
}
}
]
}
}
我想得到
[
{
"id": "112",
"type": "pro",
"email": "liz@google.com"
},
{
"id": "2",
"type": "pro",
"email": "john@google.com"
}
]
检查此 fiddle:http://jsfiddle.net/7jLz13n6/
你可以这样做:
var output=[];
var input=o.hits.hits;
for(i=0;i<input.length;i++)
output.push(input[i]._source);
console.log(output);