当我有JSON对象列表时,如何从唯一键返回值?

时间:2019-07-02 14:37:35

标签: javascript json

我有一个JSON对象列表,例如,我想使用名为“ 91842301”的键来访问该对象。请参见下面的代码。

我该怎么做?

我尝试使用诸如find,where,propertyOf和其他函数,但是这些没有考虑到我正在搜索的对象列表,而这些对象我只知道其键而不是列表中的位置。 / p>

[{
  "918345787": [
    {
      "year": "2018",
      "name": "Lucidtech AS",
      "organizationNumber": "918345787",
      "employees": "5",
      "currency": "NOK",
      "totalOperatingIncome": 481000,
      "personnelExpenses": 900000,
      "salaryCosts": 741000,
      "operatingProfit": -1043000,
      "netIncome": -1042000,
      "timestamp": "2019-7-2 13:30:16",
      "daughter": "No avaliable information about daughters"
    },
    {
      "year": "2017",
      "name": "Lucidtech AS",
      "organizationNumber": "918345787",
      "employees": "unknown",
      "currency": "NOK",
      "totalOperatingIncome": 300000,
      "personnelExpenses": 813000,
      "salaryCosts": 710000,
      "operatingProfit": -622000,
      "netIncome": -621000,
      "timestamp": "2019-7-2 13:30:16",
      "daughter": "No avaliable information about daughters"
    }
  ]
},
{
  "979369867": [
    {
      "year": "2018",
      "name": "Sikom AS",
      "organizationNumber": "979369867",
      "employees": "11",
      "currency": "NOK",
      "totalOperatingIncome": 18448000,
      "personnelExpenses": 5563000,
      "salaryCosts": null,
      "operatingProfit": 275000,
      "netIncome": 56000,
      "timestamp": "2019-7-2 13:30:37",
      "daughter": "No avaliable information about daughters"
    },
    {
      "year": "2017",
      "name": "Sikom AS",
      "organizationNumber": "979369867",
      "employees": "unknown",
      "currency": "NOK",
      "totalOperatingIncome": 15467000,
      "personnelExpenses": 3453000,
      "salaryCosts": 2758000,
      "operatingProfit": -1000,
      "netIncome": 74000,
      "timestamp": "2019-7-2 13:30:37",
      "daughter": "No avaliable information about daughters"
    }
  ]
}]

3 个答案:

答案 0 :(得分:5)

您可以检查钥匙是否存在并拿走这个物体。

key = '8980243'
result = data.find(object => key in object);

答案 1 :(得分:3)

使用find在数组中查找内容。

使用in测试属性是否存在。

const that_which_is_sought = "91842301";
const found_object = your_array.find( object_in_array => that_which_is_sought in object_in_array );

答案 2 :(得分:0)

如果我很了解您想要什么,这是一个可行的示例:

let findKey = "918345787";

let array = [{
  "918345787": [
    {
      "year": "2018",
      "name": "Lucidtech AS",
      "organizationNumber": "918345787",
      "employees": "5",
      "currency": "NOK",
      "totalOperatingIncome": 481000,
      "personnelExpenses": 900000,
      "salaryCosts": 741000,
      "operatingProfit": -1043000,
      "netIncome": -1042000,
      "timestamp": "2019-7-2 13:30:16",
      "daughter": "No avaliable information about daughters"
    },
    {
      "year": "2017",
      "name": "Lucidtech AS",
      "organizationNumber": "918345787",
      "employees": "unknown",
      "currency": "NOK",
      "totalOperatingIncome": 300000,
      "personnelExpenses": 813000,
      "salaryCosts": 710000,
      "operatingProfit": -622000,
      "netIncome": -621000,
      "timestamp": "2019-7-2 13:30:16",
      "daughter": "No avaliable information about daughters"
    }
  ]
},
{
  "979369867": [
    {
      "year": "2018",
      "name": "Sikom AS",
      "organizationNumber": "979369867",
      "employees": "11",
      "currency": "NOK",
      "totalOperatingIncome": 18448000,
      "personnelExpenses": 5563000,
      "salaryCosts": null,
      "operatingProfit": 275000,
      "netIncome": 56000,
      "timestamp": "2019-7-2 13:30:37",
      "daughter": "No avaliable information about daughters"
    },
    {
      "year": "2017",
      "name": "Sikom AS",
      "organizationNumber": "979369867",
      "employees": "unknown",
      "currency": "NOK",
      "totalOperatingIncome": 15467000,
      "personnelExpenses": 3453000,
      "salaryCosts": 2758000,
      "operatingProfit": -1000,
      "netIncome": 74000,
      "timestamp": "2019-7-2 13:30:37",
      "daughter": "No avaliable information about daughters"
    }
  ]
}]

//Traditional form
for (let i=0; i<array.length; i++) {
  if (array[i][findKey] != undefined) {
    console.log("Traditional From:");
    console.log(array[i][findKey]);
  }
}

//Pro form:
 let elements = array.filter(element => element[findKey] != undefined);
 console.log("Pro Form!");
 console.log(elements);