I have 2 arrays – activeDealers
and normalDealers
.
let normalDealers = [
{ nickname: 'one', inactive: false, dealer: true },
{ nickname: 'two', inactive: false, dealer: false },
{ nickname: 'three', inactive: false, dealer: false }
]
activeDealers = [...normalDealers]
then the normalDealers
array should pass through some conditions like this
if (normalDealers.length > 1) {
const prev = normalDealers.shift();
normalDealers.push(prev);
}
After this, the normalDealers
array will be like this:
[
{ nickname: 'two', inactive: false, dealer: false },
{ nickname: 'three', inactive: false, dealer: false },
{ nickname: 'one', inactive: false, dealer: true }
]
I want to change the normalDealers
array’s 0-th index obj value to
- { nickname: 'two', inactive: false, dealer: true }
[
{ nickname: 'two', inactive: false, dealer: true },
{ nickname: 'three', inactive: false, dealer: false },
{ nickname: 'one', inactive: false, dealer: false }
]
But I want the original order of the array inside the activeDealers
with updated dealer status value.
Expecting output:
[
{ nickname: 'one', inactive: false, dealer: false },
{ nickname: 'two', inactive: false, dealer: true },
{ nickname: 'three', inactive: false, dealer: false }
]
2
Answers
You can use the
unshift
method.The
unshift
method inserts new elements at the beginning of an array,and the
pop
method adds new elements to the end of the array, both return the new length of the array.This should work:
It just replaces the entry with the corresponding
nickname
inactiveDealers
with the entry innormalDealers
, assuming that eachnickname
occurs only once. I’m not sure if that’s the best way to achieve what you’re aiming for, though. To determine that, you would have to describe what the meaning behind it is. Why are two arrays needed at all?