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:
Put it in a single string with command separating the results (they just print to the console individually).
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!
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.
Comments