-1

I have a list of JSON objects, and I want to access the one with the key called "91842301" for example. See the code below.

How can I do this?

I have tried using functions such as find, where, propertyOf and others, but these do not take into consideration that I am searching through a list of objects that I know only the key of and not the placement in the list.

[{
  "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"
    }
  ]
}]
MR97
  • 1
  • 1
  • please add some data or result which match the data. – Nina Scholz Jul 02 '19 at 14:39
  • 3
    `.find` shouldn't fail. It is designed for *precisely* this situation - looking through a list of items. – VLAZ Jul 02 '19 at 14:41
  • The data is there, I don't understand what you mean. – MR97 Jul 02 '19 at 14:41
  • @VLAZ, I have the data stored as a list of JSON objects. When I write var hello = mydata.find(91884395); it says that "91884395 is not a function. I also tried with "" around it. – MR97 Jul 02 '19 at 14:46
  • 1
    @MR97 well...that *isn't* a function. You are supposed [to pass in a predicate function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). – VLAZ Jul 02 '19 at 14:47
  • Ah! That solved my problem I believe! Thank you! – MR97 Jul 02 '19 at 14:50
  • @PM77-1 No, because that problem wanted the first occurrence of something, which I felt was different. – MR97 Jul 02 '19 at 15:00

3 Answers3

5

You could check if the key exists and take this object.

key = '8980243'
result = data.find(object => key in object);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

Use find to find something in an array.

Use in to test if a property exists.

const that_which_is_sought = "91842301";
const found_object = your_array.find( object_in_array => that_which_is_sought in object_in_array );
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

If I understood well what you want, here is a workable example:

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);
Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130