如何将嵌套对象添加到结果数组

时间:2020-09-01 04:56:53

标签: javascript node.js json reactjs rest

我已将其简化为易于阅读,但可以想象是否有1000个这样的对象。 我将如何获得“ ._source”? 我正在尝试类似的方法,但我知道这是错误的,但我正在尝试找出正确的方法。

   cd ~/ffmpeg_sources && \
   wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 && \
   tar xjvf ffmpeg-snapshot.tar.bz2 && \
   cd ffmpeg && \
   PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
     --prefix="$HOME/ffmpeg_build" \
     --pkg-config-flags="--static" \
     --extra-cflags="-I$HOME/ffmpeg_build/include" \
     --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
     --extra-libs="-lpthread -lm" \
     --bindir="$HOME/bin" \
     --enable-gpl \
     --enable-gnutls \
     --enable-libaom \
     --enable-libass \
     --enable-libfdk-aac \
     --enable-libfreetype \
     --enable-libmp3lame \
     --enable-libopus \
     --enable-libvorbis \
     --enable-libvpx \
     --enable-libx264 \
     --enable-libx265 \
     --enable-nonfree && \
   PATH="$HOME/bin:$PATH" make && \
   make install && \
   hash -r

4 个答案:

答案 0 :(得分:1)

您可以简单地访问对象的“ _source”属性,即el._source。用那里的价值做您想做的事。

const response = [{
  "_index": "mag",
  "_type": "_doc",
  "_id": "M6RRVXIButyqFO_PBdiU",
  "_score": 12.512785,
  "_source": {
    "paper_id": "2029523142"
  }
}, {
  "_index": "mag",
  "_type": "_doc",
  "_id": "CR3VNnIButyqFO_PY3B1",
  "_score": 12.149078,
  "_source": {
    "paper_id": "2499756461"
  }
}, ];

const data = response.map(el => el._source);

console.log(data);

答案 1 :(得分:1)

您可以简单地使用for loop

var obj = [{"_index":"mag","_type":"_doc","_id":"M6RRVXIButyqFO_PBdiU","_score":12.512785,"_source":{"paper_id":"2029523142"}},{"_index":"mag","_type":"_doc","_id":"CR3VNnIButyqFO_PY3B1","_score":12.149078,"_source":{"paper_id":"2499756461"}}, ];



for(var k in obj){
  console.log( '_source=>', obj[k]._source);
}

答案 2 :(得分:1)

您可以使用

ans

of只需使用循环

答案 3 :(得分:1)

尝试:

let array = [{"_index":"mag","_type":"_doc","_id":"M6RRVXIButyqFO_PBdiU","_score":12.512785,"_source":{"paper_id":"2029523142"}},{"_index":"mag","_type":"_doc","_id":"CR3VNnIButyqFO_PY3B1","_score":12.149078,"_source":{"paper_id":"2499756461"}} ]
  
var targetValues = array.map(value => value._source);

myObj.map(item=>item._source)