A
A
Alex102142021-06-20 17:32:41
JavaScript
Alex10214, 2021-06-20 17:32:41

How to select objects by the value of another object?

Good afternoon. I have an object:

obj = {
       ticker: "SPY",
       strategy: "LOG", 
       broker: ""
}

And there is a list of objects:
const a = {
        ticker: 'SPY',
        strategy: 'OLD',
        broker: 'OOH'
      };

const b = {
        ticker: 'SPY',
        strategy: 'LOG',
        broker: 'FOR'
      };

const c = {
        ticker: 'HG',
        strategy: 'KIP',
        broker: 'LOOP'
      };


tell me how to return objects whose "tiker" and "strategy" properties match the obj object property?
In this example, only object b matches.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pashenka, 2021-06-20
@Alex10214

const a = {
        ticker: 'SPY',
        strategy: 'OLD',
        broker: 'OOH'
      };

const b = {
        ticker: 'SPY',
        strategy: 'LOG',
        broker: 'FOR'
      };

const c = {
        ticker: 'HG',
        strategy: 'KIP',
        broker: 'LOOP'
      };

const obj = {
       ticker: "SPY",
       strategy: "LOG", 
       broker: ""
};

const objs = [a, b, c];
let out = [];

for (let i = 0; i < objs.length; i++) {
    if (objs[i].ticker === obj.ticker && objs[i].strategy === obj.strategy) {
        out.push(objs[i]);    
    }
}
console.log(out);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question