Open In App

How to call the map method only if the element is an array?

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn How to call the map method only if the element is an array, Given an object literal, let us say ‘person’. It has a number of properties of various types. The task here is to map the values of the property only if the property is an Array itself. Instead of an object, we can also perform the same set of operations on an array. 

To achieve the above task, we can use the Array filter() method in JavaScript and then subsequently map the results. 

The Array filter() method is used to create a new array from a given array consisting of only those elements which satisfy a condition set by the argument function. 

The Array map() method in JavaScript creates an array by calling a specific function on each element present in the parent array.

Steps:

  • Convert the object values into an array using the Object.values() method. Let it be arr.
  • Iterate over the array(arr) using the filter method. The filter returns true if the type of the element is ‘object’.
  • Map the resulting array using the map method.

Following are the variations of the given problem:

Example 1:Mapping From an object literal the issue with the above-mentioned approach lies in the fact that neither the filter nor the map method can be used to iterate objects. To work around this problem, Object.Values should be used. Object.Values take an object as a parameter and return an array of all the values in that object. Now we can apply array map() and array filter() methods on this array.

const person = {
    first_name: 'John',
    last_name: 'Doe',
    skill_set: ['C++', 'python', 'java',
                'javascript', 'pascal', 'C#'],
    fav_numbers: [10, 19, 17, 62.98, 76, 32.9],
    email: 'john@someplace.com'
}
 
 Output: (2) [Array(6), Array(6)]
      0: (6) ["C++", "python", "java",
              "javascript", "pascal", "C#"]
      1: (6) [10, 19, 17, 62.98, 76, 32.9]

Example:

Javascript




// The object literal Person from which we
// need to extract the arrays
const Person = {
    first_name: 'John',
    last_name: 'Doe',
    skill_set: ['C++', 'python', 'java',
        'javascript', 'pascal', 'C#'],
    fav_numbers: [10, 19, 17, 62.98, 76, 32.9],
    email: 'john@someplace.com'
}
 
// Method 1 : Object.values converts the object
// literal into an array of its values
const result = Object.values(Person).filter(function (per) {
    //typeof array in javascript is 'object'
    // we can also do (typeof per) === (typeof []) here
    return (typeof per) === 'object';
}).map(function (per) {
    return per;
})
 
//output the array containing the required arrays
console.log(result);


Output:

call the map method only if the element is an array

Console Output

Example 2: Mapping From an array, we can directly use array map() and array filter() methods here.  

const Person = ['John',8.6,['C++', 'python', 'java', 
                     'javascript', 'pascal', 'C#' ],
   'john@someplace.com',[10, 19, 17, 62.98, 76, 32.9]];
 
 Output: (2) [Array(6), Array(6)]
         0: (6) ["C++", "python", "java", 
                 "javascript", "pascal", "C#"]
         1: (6) [10, 19, 17, 62.98, 76, 32.9]

Example:

Javascript




const person = ['John', 8.6, ['C++', 'python', 'java',
    'javascript', 'pascal', 'C#'],
    'john@someplace.com', [10, 19, 17, 62.98, 76, 32.9]];
 
const res = person.filter(function (per) {
    // The typeof array in javascript is 'object'
    // We can also do (typeof per) === (typeof []) here
    return (typeof per) === 'object';
}).map(function (per) {
    return per;
})
 
// Output the array containing the required arrays
console.log(res);


 Output:

call the map method only if the element is an array

Console Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads