0

I am querying a database and it is returning all of the objects in a collection called contracts. These contracts are simple objects with keys and values. Such as

{
name: BOB,
title: theinternet
date: {...}
}

I would like to check specific keys in each of the objects but I am not sure how to properly describe how these objects are grouped together, and I don't know how to access any of the data inside of this collection of objects.

console.log(contracts)
returns  

Array(4) [ {…}, {…}, {…}, {…} ] this spreads to 


(4) […]
​
0: Object { id: "1K19Q7tXIM2yJih7Qj4y", amount: "123421", company: "43214", … }
​
1: Object { id: "MvEr5t9Sd0oqDyajN9rl", amount: "663425", company: "tewq", … }
​
2: Object { id: "ckgOLDU6RdstsIoraKiy", amount: "123421", company: "43214", … }
​
3: Object { id: "jr6XjkAntRucmx0EPeQC", amount: "134", company: "rewq", … }

However, when I console.log(typeof contracts) it says it's an object.

I was able to get the size of the object correctly by doing the following... but I don't know how to search for a specific key in that object.

Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key)) size++;
        }
        return size;
    };
    console.log(Object.size(contracts))

returns 4

Once I have all of the objects I would like to be able to if(obj.title === 'theinternet' then do something. But I am unfamiliar with de-structuring this type of object.

9
  • 2
    Array in JS is of type object (confusingly), but it has a length property you could use instead of your Object.size function Commented Oct 22, 2019 at 19:17
  • @thedude If I try to do console.log(contracts.length) It throws an error saying that contracts doesn't exist. Commented Oct 22, 2019 at 19:19
  • 1
    that means contracts does not exist when you try to console.log it, not the length property Commented Oct 22, 2019 at 19:21
  • @thedude If I remove the .length from console.log(contracts.length) and just print contracts, it prints out all of the contracts. I am very confused. Commented Oct 22, 2019 at 19:24
  • 1
    me too... If you want you can ask a separate question and provide some more details there Commented Oct 22, 2019 at 19:25

2 Answers 2

1

You can use a filter.

let newObj = contracts.filter(item => item.title === 'theinternet')
Sign up to request clarification or add additional context in comments.

3 Comments

This would be syntactically incorrect. Either should be implicit return contracts.filter(item => item.title === 'theinternet') or return with curly braces contracts.filter(item => { return item.title === 'theinternet' })
I get contracts is undefined when I try this method. I feel as though this object is not a traditional object but I'm not sure.
@AaronEtheridge This assumes that you set your object variable name to contracts. You can set it to whatever you like or pass the object itself into the function. Based on the data you posted, this will work.
1

There are two ways to do it, if you only want one item like firstOrDefault on linq you will use find(), if you want all the items that match your Id you will use filter(). You can also use map or anything else but filter or find will make more sense.

var data = [{
        'id': '1K19Q7tXIM2yJih7Qj4y',
        'amount': '123421',
        'company': '43214'
   },
   {
        'id': 'MvEr5t9Sd0oqDyajN9rl',
        'amount': '663425',
        'company': 'tewq'
   },
   {
        'id': 'ckgOLDU6RdstsIoraKiy',
        'amount': '123421',
        'company': '43214'
   },
   {
        'id': 'jr6XjkAntRucmx0EPeQC',
        'amount': '134',
        'company': 'rewq'
   }
   ];

   function filterObject(filterParam) 
   {
    return data.filter(a=> a.id == filterParam)
   }

Pass the parameter on here.

   console.log(filterObject('1K19Q7tXIM2yJih7Qj4y'));

This way will return all the items, try using find() too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.