访问JSON对象键/值,其中值是使用underscorejs的数组

时间:2020-11-29 23:46:59

I have a local JSON dataset (outlined below) and am trying to use the _.where method to retrieve specific values from within the dataset.

我有一个本地JSON数据集(概述如下),并试图使用_。从数据集中检索特定值的方法。

JSON File

JSON文件

"data": [{
    "singles_ranking": [116],
    "matches_lost": ["90"],
    "singles_high_rank": [79],
    "matches_won": ["170"],
    "singles_ranking/_source": ["116"],
    "year_matches_won": ["11"],
    "name": ["Pfizenmaier Dinah"],
    "gender": ["woman"],
    "_resultNumber": 1,
  },{etc},{etc}];

Currently I am trying to retrieve values from within the dataset like so:

目前我正在尝试从数据集中检索值,如下所示:

var mappedPlayers = _.map(players,function(key,val){return key});
var filteredPlayers = _.where(mappedPlayers, {name:'Pfizenmaier Dinah'}); 
console.log(filteredPlayers);

This currently returns undefined. I am 90% sure that this is because the key values are stored within an array however, I am not sure how I can modify this _.where condition to actually make it return the text within the value attribute.

这个目前返回定义。我90%确信这是因为键值存储在数组中,但是我不确定如何修改这个_。当条件实际使它返回值属性中的文本时。

Any help would be greatly welcomed. Thank you for reading!

任何帮助都会受到极大的欢迎。感谢您的阅读!

1 个解决方案

#1


1  

With ._where it is not possible, but you can use _.filter, like so

有。_where是不可能的,但是你可以用_。过滤器,这样

var where = {key: 'name', value: 'Pfizenmaier Dinah'};
var filteredPlayers = _.filter(players, function (el) {
    // check if key exists in Object, check is value is Array, check if  where.value exists  in Array  
    return el[where.key] && _.isArray(el[where.key]) && _.indexOf(el[where.key], where.value) >= 0;
});

Example

例子

#1


1  

With ._where it is not possible, but you can use _.filter, like so

有。_where是不可能的,但是你可以用_。过滤器,这样

var where = {key: 'name', value: 'Pfizenmaier Dinah'};
var filteredPlayers = _.filter(players, function (el) {
    // check if key exists in Object, check is value is Array, check if  where.value exists  in Array  
    return el[where.key] && _.isArray(el[where.key]) && _.indexOf(el[where.key], where.value) >= 0;
});

Example

例子