6

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!

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
tbowden
  • 1,008
  • 1
  • 19
  • 44

5 Answers5

5

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
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
4

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);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
2

Using the map function

const cuisineTypes = [{
    "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"
  }
];

const result = cuisineTypes.map(x => x.cuisineType);

console.log(JSON.stringify(result, null, 4));
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
2

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.

xjanus
  • 21
  • 2
0

With a bit of ES6:

  function extractArrayFromKey(ArrayOfObjects,WantedKey){
     return ArrayOfObjects(obj => {
       let val = null;
       Object.entries(obj).forEach(([key,value]) => {if (key==WantedKey) val=value});
       return val
    })
  }

 const newArray = extractArrayFromKey(arr,'cuisineType') 

 -> ["Indian", "Italian", "Asian", "Japanese", "African"]
Banzy
  • 1,590
  • 15
  • 14