Extracting all values from a Array of Objects based on keys value

tbowden

If I have an array that looks like below:

[{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
 {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}]

How can I sort this by cuisineType?

I have tried to use:

var result = array.find(obj => {
var go =  obj.cuisineType
console.log(String(obj.cuisineType))
})

However I cannot figure out how to:

  1. Put it in a single string with command separating the results (they just print to the console individually).

  2. use the string I just made as console.log(go) or console.log(result) prints 'undefined'.

Thanks for your help in advance! I've tried other suggestions on SO and beyond but haven't had much success!

Rory McCrossan

The simplest thing to do would be to use map() to build an array of the cuisines. Then you can loop through it or build a string from it as required.

var arr = [{"matchedKey":"cuisineType","cuisineType":"Indian","group":"group"},
  {"matchedKey":"cuisineType","cuisineType":"Italian","group":"group"},
  {"matchedKey":"cuisineType","cuisineType":"Asian","group":"group"},
  {"matchedKey":"cuisineType","cuisineType":"Japanese","group":"group"},
  {"matchedKey":"cuisineType","cuisineType":"African","group":"group"}]

var cuisines = arr.map(function(el) {
  return el.cuisineType;
});

console.log(cuisines); // array
console.log(cuisines.join(', ')); // formatted string

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get all keys in array of objects whose values are arrays

Extracting values from Array of nested Objects in JavaScript

How to get objects with unique values for specific keys from array of objects?

Extracting unique values from an array of objects where value is a nested object in JavaScript

Getting all keys & their values from nested objects in JSON array (w/out jQuery)

Extracting values from array

Removing elements from an array of objects based on duplicate values of multiple keys

Retrieve all the keys in an array of objects with a particular value

Extracting values of unknown keys in an array of objects

javascript remove all objects from array based on property value

Filter an array of objects based on array property containing all values from another array

Extracting data from an array of JSON objects for specific object values

Problem extracting values from an array of objects in TypeScript

how to collect data from json objects with multiple keys and push all values to single key:value array

Extracting key value from object into array of objects with specific fields

Extracting all the values from ONLY a column as list based on an if condition pandas

Search in array of objects in JavaScript in all keys values

filter array of objects based on separate object values by keys using reactjs

Extracting single value from objects in the JSON array and writing to a range in Sheets

Extracting values from array nested inside array of objects in javscript

Remove empty values from array of objects only if the value is empty in all objects

Create objects from array of objects based on equal keys value Javascript

Grouping values into one array of objects from two objects based on differing keys

Sorting an Objects keys/values by an array of objects the object is created from

Group objects from an array of objects based on two or more keys and from an array of dynamically collected values

Extracting keys from JMESPath expression in objects

extracting info from array objects

Filter array of objects by values when not all objects have some keys

Javascript: Get only duplicate values from an array of objects based on several key value pairs (but not all)