Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 234
tbowden Avatar asked Dec 17 '18 11:12

tbowden


3 Answers

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
like image 181
Rory McCrossan Avatar answered Oct 17 '22 13:10

Rory McCrossan


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);
like image 28
Jack Bashford Avatar answered Oct 17 '22 14:10

Jack Bashford


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.

like image 2
xjanus Avatar answered Oct 17 '22 13:10

xjanus