0

I am having an array with name of the product and its storage

let array = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storage: "256GB"}, {name: "samsung A", storage: "256GB"}] 

I have another array which has all the names which needed storage let array be

let array1 = ["samsung A", "samsung B", "samsung C"] 

Comparing the above 2 arrays i want to map each key what are the available storages.

I have tried with

array.filter(el => el.name === "samsung A").map(item => item.storage) 

But in the above i manually given "samsung A" but i want to get all the storages available in array from all the keys available in array2

2
  • Sorry I am not able to understand the question. You want to return an array of storage matching with array2 from array 1CommentedFeb 26, 2024 at 10:46
  • yes, want to filter all the storages available for the specific brand name available in array2
    – Siva Sai
    CommentedFeb 26, 2024 at 10:47

4 Answers 4

0

A simple approach would be:

  1. Loop over all the phones you want to have (previous called array2)
  2. Filter out your original data set by the name of that phone
  3. Get all storages from the different data sets
  4. Add this new value to the result

let data = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storage: "256GB"}, {name: "samsung A", storage: "256GB"}] let phones = ["samsung A", "samsung B", "samsung C"] // Loop all phones let result = phones.map(phone => { // Get all data by name let filteredData = data.filter(d => d.name === phone) // Map all storages let storages = filteredData.map(fd => fd.storage) return {name: phone, storages: storages} }) console.log(result)

    0

    To start off, you will need a storage map, which maps each device to an array of storage capacities, e.g. {'samsung A': ['128GB', '256GB'] }.

    Then, for each product name in array1 (i.e. "samsung A", "samsung B", "samsung C"), get the corresponding storage value(s) in array (i.e. filter array1 to all the keys named after the value on the current iteration and map them to an array).

    In the end, you add a new entry to the storage map which the array returned from the step above.

    let storage = {}; array1.forEach(product => { let storages = array.filter(item => item.name === product).map(item => item.storage); storage[product] = storages; }) 

    You can also use .reduce:

    let storage = array1.reduce((acc, product) => { acc[product] = array.filter(item => item.name === product).map(item => item.storage); return acc; }, {}); 
      0

      At the first run map on array1 and then filter on array

      let result = [] array1.map(arr => { let temp = array.filter(el => el.name === arr).map(item => item.storage) result.push({name:arr, storages:temp}) }) console.log(result); 
      1
      • It seems like you want to filter specific key values from an array based on the values of another key. Your code looks mostly correct, but there's a minor issue with the filtering logic. You need to check if the name property of the elements in array matches with the current element arr from array1. Here's the corrected version of your code: let result = []; array1.forEach(arr => { let temp = array.filter(el => el.name === arr).map(item => item.storage); result.push({ name: arr, storages: temp }); }); console.log(result);
        – Savad
        CommentedFeb 27, 2024 at 6:10
      0

      As per my understanding

      let array = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storage: "256GB"}, {name: "samsung A", storage: "256GB"}] let array1 = ["samsung A", "samsung B", "samsung C"] let finalArray = [] let storageArray = array.filter((x)=>array1.includes(x.name)).map((y)=>{ let index = finalArray.findIndex((m)=>m.name === y.name) if(index >=0){ finalArray[index].storage.push(y.storage) }else{ finalArray.push({name: y.name, storage:[y.storage]}) } }) console.log(finalArray)

      3
      • Comparing the above 2 arrays i want to map each key what are the available storages. You did not do this, you just give an array with the storages.CommentedFeb 27, 2024 at 7:49
      • Sorry for misunderstanding, Please check the answer above, considering loops used it is more efficient.CommentedFeb 28, 2024 at 9:08
      • This indeed works but isn't the most performant. However, I removed the downvote :)CommentedFeb 28, 2024 at 9:42

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.