-4

Need to merge other country cities data into single object. my inputs are below

result = {
  "Bangalore": [
    {
      "Type": "Sale",
      "Date": "2021-07-10",
    },

    {
      "Type": "Product",
      "Date": "2021-07-03",
    }
  ],
  "Delhi": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
  "Chennai": [
    {
      "Type": "Production",
      "Date": "2021-06-30",
    },
    {
      "Type": "Production",
      "Date": "2021-07-11",
    },
  ],
  "Goa": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
    "sydney": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
    "melbourne": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ]

}

I have other country cities array is below

let arr = [
    "sydney",
    "melbourne"
  ]

I tried with below snippet its not working.

  let {sydney,melbourne,...final}=result;

let othercity = [...sydney,...melbourne]

console.log(final)

Actually When I pass variable into let {sydney,melbourne,...final} its working as expected. But I want to pass this variable sydney,melbourne dynamically. ie 'let {...arr,...final}=result;'

Expected output results: IndiaCities

{
  "Bangalore": [
    {
      "Type": "Sale",
      "Date": "2021-07-10"
    },
    {
      "Type": "Product",
      "Date": "2021-07-03"
    }
  ],
  "Delhi": [
    {
      "Type": "Product",
      "Date": "2021-06-30"
    }
  ],
  "Chennai": [
    {
      "Type": "Production",
      "Date": "2021-06-30"
    },
    {
      "Type": "Production",
      "Date": "2021-07-11"
    }
  ],
  "Goa": [
    {
      "Type": "Product",
      "Date": "2021-06-30"
    }
  ]
}

otherCitys

{"othercities":[
  {
     "Type": "Product",
     "Date": "2021-06-30"
   },
   {
     "Type": "Product",
     "Date": "2021-06-30"
   }
]}

How to resolve this destructing concept. your answer is greatly appreciated

let result = {
  "Bangalore": [
    {
      "Type": "Sale",
      "Date": "2021-07-10",
    },

    {
      "Type": "Product",
      "Date": "2021-07-03",
    }
  ],
  "Delhi": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
  "Chennai": [
    {
      "Type": "Production",
      "Date": "2021-06-30",
    },
    {
      "Type": "Production",
      "Date": "2021-07-11",
    },
  ],
  "Goa": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
    "sydney": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ],
    "melbourne": [
    {
      "Type": "Product",
      "Date": "2021-06-30",
    }
  ]

}

let arr = [
    "sydney",
    "melbourne"
  ]
  
  let {sydney,melbourne,...final}=result;

let othercity = [...sydney,...melbourne]

  console.log(othercity)

console.log(final)
Jay
  • 187
  • 2
  • 13

1 Answers1

1

You are correct when you are using

const { sydney, melbourne, ...indianCities } = result;

since you want to accumulate the result into an object that contain a property otherCities which is an array of two object i.e. sydney and melbourne

const othercitiesObj = {
  otherCities: [...sydney, ...melbourne],
};

const result = {
  Bangalore: [
    {
      Type: "Sale",
      Date: "2021-07-10",
    },

    {
      Type: "Product",
      Date: "2021-07-03",
    },
  ],
  Delhi: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  Chennai: [
    {
      Type: "Production",
      Date: "2021-06-30",
    },
    {
      Type: "Production",
      Date: "2021-07-11",
    },
  ],
  Goa: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  sydney: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  melbourne: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
};

let arr = ["sydney", "melbourne"];

const { sydney, melbourne, ...indianCities } = result;
const othercitiesObj = {
  otherCities: [...sydney, ...melbourne],
};

console.log(othercitiesObj);

Edited: Since you want dynamic city

const result = {
  Bangalore: [
    {
      Type: "Sale",
      Date: "2021-07-10",
    },

    {
      Type: "Product",
      Date: "2021-07-03",
    },
  ],
  Delhi: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  Chennai: [
    {
      Type: "Production",
      Date: "2021-06-30",
    },
    {
      Type: "Production",
      Date: "2021-07-11",
    },
  ],
  Goa: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  sydney: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  melbourne: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
};

let arr = ["sydney", "melbourne"];

const { sydney, melbourne, ...indianCities } = result;
const othercitiesObj = {
  otherCities: arr.flatMap((city) => result[city]),
};

console.log(othercitiesObj);

But if there is a city that doesn't exist in the arr array then you can use reduce

const result = {
  Bangalore: [
    {
      Type: "Sale",
      Date: "2021-07-10",
    },

    {
      Type: "Product",
      Date: "2021-07-03",
    },
  ],
  Delhi: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  Chennai: [
    {
      Type: "Production",
      Date: "2021-06-30",
    },
    {
      Type: "Production",
      Date: "2021-07-11",
    },
  ],
  Goa: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  sydney: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
  melbourne: [
    {
      Type: "Product",
      Date: "2021-06-30",
    },
  ],
};

let arr = ["sydney", "melbourne", "bhuna"];

const { sydney, melbourne, ...indianCities } = result;
const othercitiesObj = {
  otherCities: arr.reduce((acc, curr) => {
    if (result[curr]) acc = [...acc, ...result[curr]];
    return acc;
  }, []),
};

console.log(othercitiesObj);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • This result I got it already but 'arr' ie array I have to pass dynamically and get the othercities data – Jay Jun 30 '21 at 14:42
  • @deckpk I fixed myself this what I expected ` let arrRef: any = []; arr.map(item => { if (result[item]) { arrRef.push(...result[item]); delete result[item]; } }); return arrRef; ` – Jay Jun 30 '21 at 17:17
  • Thanks for you help – Jay Jun 30 '21 at 17:22