0

is there a way to get values from an array by fields at once?

for example there is an array of objects

const arr = [
    {
        name: 'tomato',
        value: 20,
    },
    {
        name: 'apple',
        value: 44,
    },
    {
        name: 'mango',
        value: 36,
    }
]

from this array I need to get all objects by name. And in a result a need 3 object. The one way is to find each object in an array by name

const tomato = arr.find(item => item.name === 'tomato')
const apple = arr.find(item => item.name === 'apple')
const mango = arr.find(item => item.name === 'mango')

is there others way how can I get object from an array by name? is it possible to do at one array iteration?

5
  • Is it always going to be tomato, apple, mango? Because then you should rather use an object. Commented Jan 9, 2020 at 7:47
  • this objects store in an array Commented Jan 9, 2020 at 7:49
  • Use a regular for...of loop and store them in separate variables. Commented Jan 9, 2020 at 7:50
  • 1
    I mean like that: const fruits = {tomato: 20, apple: 44, mango: 46}; You can also map it to that form with a reduce on your current arr. Commented Jan 9, 2020 at 7:51
  • can there be duplicate tomato item? You just want to find the first one? So what if you use one iteration, and you give 3 names, then stop as soon as one is found for each name? Commented Jan 9, 2020 at 7:59

4 Answers 4

2

Using reduce() and object destructuring

const arr = [{name:"tomato",value:20},{name:"apple",value:44},{name:"mango",value:36}];

const { tomato, apple, mango } = arr.reduce((a, i) => ({...a, [i.name]: i.value}), {})

console.log(tomato, apple, mango)

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

1 Comment

would it need to handle [{name:"tomato",value:20},{name:"apple",value:44},{name:"mango",value:36}, {name:"tomato",value:10000}];
2

It is possible to use Map collection:

let uniqueArr= new Map(arr.map(s=> [s.name, s]))

An example:

const arr = [
    {
        name: 'tomato',
        value: 20,
    },
    {
        name: 'apple',
        value: 44,
    },
    {
        name: 'mango',
        value: 36,
    }
]

let uniqueArr = new Map(arr.map(s=> [s.name, s]))

console.log(uniqueArr.get('tomato'))
console.log(uniqueArr.get('apple'))
console.log(uniqueArr.get('mango'))

Comments

1

const arr = [
    {
        name: 'tomato',
        value: 20,
    },
    {
        name: 'apple',
        value: 44,
    },
    {
        name: 'mango',
        value: 36,
    }
]

function getObjects(args){

  var output = [];
  
  args.forEach(name => {
  
    arr.forEach(elem => {

      if(name === elem.name)
        output.push(elem)

    });
     
  });

  return output;
}

var args = ['mango', 'tomato'];

var objs = getObjects(args);

objs.forEach(elem => console.log(elem));

How about this ?

Comments

0
  • .flatMap() on array
  • Each object can be broken down further by Object.entries() and destructuring
  • then flatMap() each object of array
  • The final form is [key, value] arrays of each object.
  • Any match between the given property and the current key will result in the corresponding value to be returned as a sub-array which finally returns flattened (extracted from the sub-array).
  • Otherwise an empty sub-array is returned and flattened to nothing.

const data = [{
  key1: "A",
  key2: 1
}, {
  key1: "B",
  key2: 2
}, {
  key1: "C",
  key2: 3
}];

const getValuesOf = (arrayOfObjects, property) => {
  return arrayOfObjects.flatMap(object => {
    return Object.entries(object).flatMap(([key, value]) => {
      return property === key ? [value] : []
    });
  });
};

console.log(getValuesOf(data, 'key1'));
console.log(getValuesOf(data, 'key2'));

Comments

Your Answer

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