please is there a way to turn subject1 to subject2. if you notice, the chemistry in the subject2 occurs at interval of 3.
const subjects = [
{ "name": "Math", "score": 32 },
{ "name": "Chemistry", "score": 17 },
{ "name": "English", "score": 17 },
{ "name": "Math", "score": 55 },
{ "name": "Chemistry", "score": 21 },
{ "name": "Chemistry", "score": 75 },
{ "name": "Chemistry", "score": 45 },
{ "name": "Physics", "score": 9 },
{ "name": "Physics", "score": 4 },
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
{ "name": "Physics", "score": 11 },
];
const Newsubjects = [
{ "name": "Math", "score": 32 },
{ "name": "English", "score": 17 },
{ "name": "Chemistry", "score": 17 }, *********
{ "name": "Math", "score": 55 },
{ "name": "Physics", "score": 9 },
{ "name": "Chemistry", "score": 21 }, *********
{ "name": "Physics", "score": 4 },
{ "name": "Physics", "score": 21 },
{ "name": "Chemistry", "score": 75 }, *********
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
{ "name": "Chemistry", "score": 45 }, *********
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
{ "name": "Physics", "score": 21 },
{ "name": "Physics", "score": 11 },
];
SO i created this simple for loop, It works well
const subjects = [
{ "name": "Math", "score": 32 },
{ "name": "Chemistry", "score": 17 },
{ "name": "English", "score": 17 },
{ "name": "Math", "score": 55 },
{ "name": "Chemistry", "score": 21 },
{ "name": "Chemistry", "score": 75 },
{ "name": "Chemistry", "score": 45 },
{ "name": "Geo", "score": 9 },
{ "name": "Physics", "score": 4 },
{ "name": "Arab", "score": 21 },
{ "name": "Hausa", "score": 11 },
{ "name": "Yoruba", "score": 21 },
{ "name": "Commerce", "score": 11 },
{ "name": "Spanish", "score": 21 },
{ "name": "German", "score": 11 },
{ "name": "Gov", "score": 11 },
];
let filteredArray = subjects.filter(value => value.name == "Chemistry");
let filteredArray2 = subjects.filter(value => value.name != "Chemistry");
//console.log(filteredArray)
for (let i = 0; i <= filteredArray2.length; i++)
{
if (i % 3 == 0 ) {
filteredArray2.splice( i, 0, { "name": "Agric", "score": 11 } )
}
}
console.log(filteredArray2)
Then whenever I try to change the value of the splice to the filteredArray, so that it won’t be hardcoded { "name": "Agric", "score": 11 }
I added another for loop because I wanted to add the value from the filtered array
for (let i = 0; i <= filteredArray2.length; i++)
{
for (let j = 0; j <= filteredArray.length; j++)
{
if (i % 3 == 0) {
filteredArray2.splice(i,0, filteredArray[j],)
}
}
}
Please am open to suggestions or any idea
As the goal is to just rearrange the array based on the chemistry object and make it occur with the interval of 3
Thank you for your time
2
Answers
You could add some fixes to your code.
since you insert an element to an array you should increment the loop index (
i++
) so the loop would accommodate the new array length.based on your desired output you should insert at the loop index
minus 1 and skip the zero indexfrom moonwave99’s answer – usei % 3 === 2
.to get an element from the chemistry array use
Array::shift()
to get elementsbreak the loop if there’s no more chemistry elements to insert.
It’s possible to make the code at least twice faster with building the result array manually. Also avoid
Array::shift()
to mutate an array since write operations are slow.And a benchmark:
If you want to make it more flexible, write a function for it: