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
Just .map()
into a new array with regards to cuisines. Then do whatever you want with that data:
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 sortedArr = arr.map(e => e.cuisineType);
console.log(sortedArr);
If you are new to Javascript but have used any other programming languages before, this might be an easier - not efficient - way of solving your current conundrum. I have written every step of the js with appropriate comments for readability and understanding of every step.
/****** COMMENT *******
Declaring the original array as arr
*********************/
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"
}
];
/****** COMMENT *******
Declaring a new empty array
*******************/
var newArray = [];
/****** COMMENT *******
array.push() method adds one or more elements to the end of an array.
So, I'm appending values of key - cuisineType - in the empty array named newArray (see above).
*******************/
for (var i = 0; i < arr.length; i++){
/****** COMMENT *******
Through this for loop, 'i' will point as index value to each individual object within an array starting from 0 up to the (length of an array - 1) ==> in your case, 5 - 1 = 4.
*******************/
newArray.push(arr[i].cuisineType);
}
/****** COMMENT *******
join() method creates and returns a new string by concatenating all of the elements in an array
*******************/
console.log(newArray.join(', '));
The Output in your console.log will be
Indian, Italian, Asian, Japanese, African
Hope this what you were looking for. Have a great day.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With