我有某些动态数据,其中包含.jpg,.png等文件格式,除此之外,还有.zip,.rar等其他格式,因此在这里我如何过滤和仅获取.jpg,结果。 png并忽略zip和rar的结果?
例如“数据”:“ 123.jpg”
Results = {
"data": [
{
"_score": 0.36464313,
"_type": "data",
"_id": "abcd",
"_source": {
"filter": "data",
"information": {
"product_id": "abcd",
"creation_utctime": "1477335693653",
"data":"123.jpg"
},
"enduser": "free"
},
"_index": "dell_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcde",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"123.png"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcdef",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"123.rar"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcdef",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"1235.zip"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
}
],
}
通常我使用这种逻辑来获取嵌套值:
this.rows=[];
for (var res in this.Result) {
var row = {};
for (var key in this.Result[res]['_source']) {
if (typeof this.Result[res]['_source'][key] === 'object') {
for (var k in this.Result[res]['_source'][key]) {
let temp = key + "." + k;
row[temp] = this.Result[res]['_source'][key][k];
}
} else {
row[key] = this.Result[res]['_source'][key]
}
row['_id'] = this.Result[res]['_id'];
}
this.rows.push(row);
}
答案 0 :(得分:1)
您可以将Array.prototype.filter()与String.prototype.match()一起使用正则表达式。
let data= [
{
"_score": 0.36464313,
"_type": "data",
"_id": "abcd",
"_source": {
"filter": "data",
"information": {
"product_id": "abcd",
"creation_utctime": "1477335693653",
"data":"123.jpg"
},
"enduser": "free"
},
"_index": "dell_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcde",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"123.png"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcdef",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"123.rar"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
},
{
"_score": 0.36464314,
"_type": "data",
"_id": "abcdef",
"_source": {
"filter": "data",
"information": {
"product_id": "abcde",
"creation_utctime": "1477335693653",
"data":"1235.zip"
},
"enduser": "free"
},
"_index": "lenovo_laptop"
}
]
let result=data.filter(a=>a._source.information.data.match(/(\.|\/)(gif|jpe?g|png)$/i))
console.log(result)
// if you want to ignore some files do this
let ignoredresult=data.filter(a=>!a._source.information.data.match(/(\.|\/)(gif|jpe?g|png)$/i))
console.log("--------------------")
console.log(ignoredresult)