0

I need solution not using any frameworks, just plain javascript.

I have initial values that I need to search in array items:

var name = 'blah';
var click = 'double';

I have array like this:

var items =
[
    {
        name: 'blah',
        click: 'single',
        url: 'url1',
        token: 'token1'
    },
    {
        name: 'blah',
        click: 'double',
        url: 'url2',
        token: 'token2'
    },
    {
        name: 'bar',
        click: 'double',
        url: 'url3',
        token: 'token3',
    },
    {
        name: 'baz',
        click: 'single',
        url: 'url4',
        token: 'token4'
    }
];

When I find the object that contains both values, in this case:

{
    name: 'blah',
    click: 'double',
    url: 'url2',
    token: 'token2'
}

Than, I need to assign rest of values from that object to separate variables. In this case result should be:

var url = 'url2';
var token = 'token2'

The problem is that i don't know what the search values will be each time. For example on each button press, search values will be different. Different value for "name" and different value for "click".

I just want to check if it is the same across all objects in that array and assign rest of values to variables.

UPDATE:

I was thinking maybe to first create array of found objects that contain matched value from "var name = blah"

var names = items.filter(function(item) {return item.name === 'blah'});

and then search over "names" array to find what is matching to var click = "double".

var match = names.filter(function(matchitem) {return matchitem.click === "double"});
2
  • If you don't know what the keyValues are, how can you declare variables to assign the values to? Commented Aug 11, 2021 at 19:20
  • sorry for misunderstanding I was thinking I don't know on each button press, what will be values of variables I need to search for. I will correct the note. Commented Aug 11, 2021 at 19:31

3 Answers 3

1

You can use find which is a method on the Array prototype. It would work like this

var match = items.find(function(item) {
  return item.name === name && item.click === click;
}

Or written in a more modern way

const match = items.find(item => item.name === name && item.click === click);

Now if you have a match you can destructure it like below to get separate variables for the various properties.

const { url, token } = match;

The variable names need to match the keys of your object for this to work.

Sign up to request clarification or add additional context in comments.

Comments

0

Get the first matching object which contains both name and click

const item = items.find(item => item.name === name && item.click === click);
if (item){
  url = item.url;
  token  = item.token;
}

Comments

0

if something is desired maybe so.

var items = [
  {
    name: 'blah',
    click: 'single',
    url: 'url1',
    token: 'token1'
  },
  {
    name: 'blah',
    click: 'double',
    url: 'url2',
    token: 'token2'
  },
  {
    name: 'bar',
    click: 'double',
    url: 'url3',
    token: 'token3',
  },
  {
    name: 'baz',
    click: 'single',
    url: 'url4',
    token: 'token4'
  }
];
var name = 'blah';
var click = 'double';

items.forEach((item) => {
  if(item.name == name && item.click == click){
    console.log(item)
  }
})

so you have access to the item and everything.

1 Comment

good answer but I'm not sure how much processing power it takes forEach + if

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.