I have this data:
const fetchedServices = [
{
_id: "5ee71bbfa7e903bfe2b324b2",
en: {
title: 'Leasing Equipment EN',
description: 'We offer short and long term leases of both new and existing containers. New container equipment ex. works or positioned to your place of demand and existing containers from our depots.'
},
nl: {
title: 'Leasing Equipment NL',
description: 'We offer short and long term leases of both new and existing containers. New container equipment ex. works or positioned to your place of demand and existing containers from our depots.'
},
},
{
_id: "5ee73429a7e903bfe2b324b3",
en: {
title: 'Lease Purchase EN',
description: 'The Lease Purchase Option we offer usually fulfills the requirements of the shipping lines, forwarders, terminals and other companies in the Transport industry, for their long term investment in containers and related equipment.'
},
nl: {
title: 'Lease Purchase NL',
description: 'The Lease Purchase Option we offer usually fulfills the requirements of the shipping lines, forwarders, terminals and other companies in the Transport industry, for their long term investment in containers and related equipment.'
},
}
];
And I parse it with this function:
const filterDataByLanguage = (data, addLang, removeLang) => {
const arr = data.slice(0);
let results = [];
for (let i = 0; i < arr.length; i++) {
delete arr[i][removeLang];
results.push(arr[i]);
for (key in arr[i][addLang]) {
results[i][key] = arr[i][addLang][key]
}
delete arr[i][addLang];
}
return results;
}
I use this function to get data for to specific files, en.json and nl.json, like that:
let enData = filterDataByLanguage(fetchedServices, 'en', 'nl');
let nlData = filterDataByLanguage(fetchedServices, 'nl', 'en');
The problem is the first call is modify input array (data).
So my question is, how I can make input to return new instance when new function call is happening.
Thanks for any information.