1

I have JavaScript array of object with the following structure:

var arrayObj = [
  {

    "Rank": 1,
    "Title": "The Shawshank Redemption",
    "Description": "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.",
    "Runtime": 142,
    "Genre": "Crime",
    "Rating": 9.3,
    "Metascore": 80,
    "Votes": 1934970,
    "Gross_Earning_in_Mil": 28.34,
    "Director": "Frank Darabont",
    "Actor": "Tim Robbins",
    "Year": 1994
  },

  {

    "Rank": 2,
    "Title": "The Godfather",
    "Description": "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.",
    "Runtime": 175,
    "Genre": "Crime",
    "Rating": 9.2,
    "Metascore": 100,
    "Votes": 1323670,
    "Gross_Earning_in_Mil": 134.97,
    "Director": "Francis Ford Coppola",
    "Actor": "Marlon Brando",
    "Year": 1972
  }]

I want to extract a field from each object except the "Director", and get an array containing the values, for example

[[1,
    'The Shawshank Redemption',
    'Two imprisoned men bond over a number of years, finding solace and 
     eventual redemption through acts of common decency.',
    142,
    'Crime',
    9.3,
    80,
    1934970,
    28.34,
    'Tim Robbins',
    1994 ],

[ 2,
    'The Godfather',
    'The aging patriarch of an organized crime dynasty transfers control 
     of his clandestine empire to his reluctant son.',
    175,
    'Crime',
    9.2,
    100,
    1323670,
    134.97,
    'Marlon Brando',
    1972 ]]
4
  • What have you tried so far? Can you post some code or a fiddle? Commented Jun 25, 2019 at 20:27
  • Please format your code so we can see the array's shape more clearly. Also, our only guess as to what your problem is is from the title. Please elaborate on it in the description. Commented Jun 25, 2019 at 20:29
  • So array map is one thing you want to use and object values Commented Jun 25, 2019 at 20:32
  • Yes, either map or reduce. Commented Jun 25, 2019 at 20:54

1 Answer 1

1

You could exclude a key by destructuring and using the rest properties.

var array = [{ Rank: 1, Title: "The Shawshank Redemption", Description: "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.", Runtime: 142, Genre: "Crime", Rating: 9.3, Metascore: 80, Votes: 1934970, Gross_Earning_in_Mil: 28.34, Director: "Frank Darabont", Actor: "Tim Robbins", Year: 1994 }, { Rank: 2, Title: "The Godfather", Description: "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.", Runtime: 175, Genre: "Crime", Rating: 9.2, Metascore: 100, Votes: 1323670, Gross_Earning_in_Mil: 134.97, Director: "Francis Ford Coppola", Actor: "Marlon Brando", Year: 1972 }],
    result = array.map(({ Director, ...o }) => o);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.