skip to Main Content

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

3

Answers


  1. 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"]
    
    let result = [];
    
    // Loop all phones
    phones.forEach(phone => {
      // Get all data by name
      let filteredData = data.filter(d => d.name === phone)
      
      // Map all storages
      let storages  = filteredData.map(fd => fd.storage)
      
      // Create result
      let entry = {name: phone, storages: storages}
      
      result.push(entry)
    })
    
    console.log(result)
    Login or Signup to reply.
  2. 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;
    }, {});
    
    Login or Signup to reply.
  3. As per my understanding

    let array = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storage: "256GB"}, {name: "samsung D", storage: "256GB"}]
    
    let array1 = ["samsung A", "samsung B", "samsung C"]
    
    let storageArray = array.filter((x)=>array1.includes(x.name)).map((y)=>y.storage)
    
    console.log(storageArray)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search