What I want to achieve: Trying to format Array3 as follows:
- 7 Elements from Array1
- 1 Element from Array2
- If no more elements in Array1 or less than 7 elements left in Array1, add the rest of Array2
Current Code:
let Array1 = ['Pizza', 'Burger', 'Pasta', 'Eggs', 'Juice', 'Coffee', 'Water', 'Nuggets', 'Chicken', 'Sandwich']
let Array2 = ['Text 1', 'Text 2', 'Text 3', 'Text 4']
let Array3 = []
What I attempted doing:
let x = 0;
let y = 0;
for (let i = 0; i < Array1.length || i < Array2.length; i ) {
if (Array1.length > 0) {
Array3.push(Array1[i]);
x ;
if (x === 7) {
Array3.push(Array2[y]);
y ;
x = 0;
}
} else if (Array2.length > 0) {
Array3.concat(Array2);
};
};
Facing issue:
Even though the above sort of works I still face these issues:
- I feel as if it's clunky and could be of higher performance.
- I obviously want to display Array3 to the user because it's ordered but lets take this example where I progressed in the array and removed the first few arrays:
let Array1 = ['4', '5', '6', '7', '8'. '9'. '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'];
let Array2 = ['a'];
let Array3 = ['4', '5', '6', '7', '8'. '9'. '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20'];
Array3 is displayed, but what if I add b
to Array2
? if I run it by the code above it'll re-order the elements to show a
after '10' because it's the 7th element but I don't want it to do so, I want prior set values to stay the same but new ones to be ordered. similarly I want the b
to be after 14
because it's the next multiplier of 7 but it's not the 14th element. So it's kinda messed up
My approach:
I'm thinking of making the elements objects that have the following:
{
item: string,
position: number
}
Whenenver a new item is added to the loop it's added with (Array3.length 1) but I'll have a check for the remainder of 7 and skip all the multipliers of 7, if an item is from Array2
I add them strictly to the next free multiplier of 7.
Please advise me if I should go on with my approach or if there is an easier way of handling it. Please note that for the simplicty and length of the question I didn't add the actual array elements, the elements are similar to:
{
user: {name: string, id: string}
requeue: boolean
position: number
}
CodePudding user response:
You could iterate until the index for slicing is greater or equal to the length of both arrays and add later the rest of both to the result set.
function merge(a, b, sizeA, sizeB) {
const result = [];
let i = 0, j = 0;
while (i < a.length && j < b.length) {
result.push(...a.slice(i, i = sizeA), ...b.slice(j, j = sizeB));
}
result.push(...a.slice(i), ...b.slice(j));
return result;
}
const
array1 = ['Pizza', 'Burger', 'Pasta', 'Eggs', 'Juice', 'Coffee', 'Water', 'Nuggets', 'Chicken', 'Sandwich'],
array2 = ['Text 1', 'Text 2', 'Text 3', 'Text 4']
console.log(merge(array1, array2, 7, 1));
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
I created the below code snippet as per my understanding based on the 3 use case you provided at the top in the post. Let me know if any further changes you required.
Working Demo :
// Input array1
let Array1 = ['Pizza', 'Burger', 'Pasta', 'Eggs'];
// Input array 2
let Array2 = ['Text 1', 'Text 2', 'Text 3', 'Text 4'];
// Result array 3
let Array3 = [];
/**
* mergeArray() is used to merge two arrays and will return newArray.
*/
function mergeArray(arr1, arr2) {
// Here we are checking if arrays contains array1 & array2 length and based on that creating newArray.
if (arr1.length === 7 && arr2.length === 1) {
Array3 = [...arr1, arr2];
} else if (arr1.length < 7 && arr2.length) {
const arr = [ ...arr1, ...arr2];
Array3 = (arr.length === 7) ? arr : [ ...arr1, ...arr2].slice(0, 7);
}
return Array3;
};
// Result
console.log(mergeArray(Array1, Array2));