let's say I have the following data array
["New York", "Madrid", "Roma"]
And I have 2 other variables and another array
firstValue = '';
middlesValues = []
lastValue = ''
output
firstValue = 'New York'
middlesValues = ['Madrid']
lastValue = 'Roma'
And I would like for example to put the first value in the array each time in a variable, the middle values in an array and the last value in the last variable. And if I have only two values in the array, to put only the first value and the last one in the 2 variables.
How can I proceed.
CodePudding user response:
Use array.slice to get the subarray, returns an empty array if you only have 2 elements.
const array= ["New York", "Madrid", "Roma"];
const firstValue = array[0];
const middlesValues = array.slice(1, array.length - 1);
const lastValue = array[array.length - 1];
console.log(firstValue);
console.log(middlesValues);
console.log(lastValue);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Use Array#shift
to get the first element, Array#pop
to get the last, and the rest would be the middle ones:
const partition = (arr = []) => {
const array = [...arr];
const firstValue = array.shift();
const lastValue = array.pop();
const middlesValues = array;
return { firstValue, middlesValues, lastValue};
}
console.log( partition(["New York", "Madrid", "Boston", "Roma"]) );
console.log( partition(["New York", "Madrid", "Roma"]) );
console.log( partition(["New York", "Madrid"]) );
console.log( partition(["New York"]) );
console.log( partition([]) );
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
The approach depends on whether the OP wants the original data to be mutated or not.
The shortest non mutating way is to use a combination of array destructuring and pop
...
let [firstValue, ...middlesValues] = array;
let lastValue = middlesValues.pop();
possible approaches could be ...
// - approach does not mutate the original data.
// - it does so by using array destructuring and `pop`.
let sampleData = ["New York", "Madrid", "Roma", "Paris"];
let [firstValue, ...middlesValues] = sampleData;
let lastValue = middlesValues.pop();
console.log({ firstValue, lastValue, middlesValues, sampleData });
sampleData = ["New York", "Madrid"];
[firstValue, ...middlesValues] = sampleData;
lastValue = middlesValues.pop();
console.log({ firstValue, lastValue, middlesValues, sampleData });
console.log('\n');
// - approach does not mutate the original data.
// - it does so by uitlizing `at` and `slice`.
sampleData = ["New York", "Madrid", "Roma", "Paris"];
firstValue = sampleData.at(0);
lastValue = sampleData.at(-1);
middlesValues = sampleData.slice(1, -1);
console.log({ firstValue, lastValue, middlesValues, sampleData });
sampleData = ["New York", "Madrid"];
firstValue = sampleData.at(0);
lastValue = sampleData.at(-1);
middlesValues = sampleData.slice(1, -1);
console.log({ firstValue, lastValue, middlesValues, sampleData });
console.log('\n');
// - approach does mutate the original data.
// - it does so by by choosing the proven way of `shift` and `pop`.
sampleData = ["New York", "Madrid", "Roma", "Paris"];
firstValue = sampleData.shift();
lastValue = sampleData.pop();
middlesValues = sampleData;
console.log({ firstValue, lastValue, middlesValues, sampleData });
console.log(
'(middlesValues === sampleData) ?..',
(middlesValues === sampleData)
);
sampleData = ["New York", "Madrid"];
firstValue = sampleData.shift();
lastValue = sampleData.pop();
middlesValues = sampleData;
console.log({ firstValue, lastValue, middlesValues, sampleData });
console.log(
'(middlesValues === sampleData) ?..',
(middlesValues === sampleData)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>