EDIT - 3 elegant solutions
Three solutions, from odinho, Pranav and Bekim. Thanks, tested and worked perfectly.
One (@odinho - very cool)
data.push(...data.splice(data.findIndex(v => v.name == 'other'), 1))
Two (also great one liner)
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
Three
var res = data.slice(),
len = res.length;
for (var i = 0; i < len; i++) {
if (res[i].name == 'other') {
res.push(res.splice(i, 1)[0]);
i--;
len--;
}
}
JS TOOLS IM USING
Angular 1.5.6, lodash 4.1x
Here is the scenario I have an array of objects sorted alphabetically e.g. sortedData below etc.. However, within that array is also the catch all Other
which is obviously sorted alphabetically as well. I want to remove other from the array and then move to the end of array without messing with the current sorted array.
NOTE
My current approach below works but is ugly. Does JS, angular or lodash have something more elegant?
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
function move(array, fromIndex, toIndex) {
array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
return array;
}
var moved = move(
data,
_.findIndex(data, ['name', 'other']),
Object.keys(data).length
);
Ideal Outcome
var data = [
{id:1,name:'one'},
{id:2,name:'two'},
{id:4,name:'four'},
{id:5,name:'five'}
{id:3,name:'other'},
]
EDIT - 3 elegant solutions
Three solutions, from odinho, Pranav and Bekim. Thanks, tested and worked perfectly.
One (@odinho - very cool)
data.push(...data.splice(data.findIndex(v => v.name == 'other'), 1))
Two (also great one liner)
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
Three
var res = data.slice(),
len = res.length;
for (var i = 0; i < len; i++) {
if (res[i].name == 'other') {
res.push(res.splice(i, 1)[0]);
i--;
len--;
}
}
JS TOOLS IM USING
Angular 1.5.6, lodash 4.1x
Here is the scenario I have an array of objects sorted alphabetically e.g. sortedData below etc.. However, within that array is also the catch all Other
which is obviously sorted alphabetically as well. I want to remove other from the array and then move to the end of array without messing with the current sorted array.
NOTE
My current approach below works but is ugly. Does JS, angular or lodash have something more elegant?
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
function move(array, fromIndex, toIndex) {
array.splice(toIndex, 1, array.splice(fromIndex, 1)[0]);
return array;
}
var moved = move(
data,
_.findIndex(data, ['name', 'other']),
Object.keys(data).length
);
Ideal Outcome
var data = [
{id:1,name:'one'},
{id:2,name:'two'},
{id:4,name:'four'},
{id:5,name:'five'}
{id:3,name:'other'},
]
Share
Improve this question
edited Aug 12, 2023 at 18:00
Nolan
asked Jun 10, 2016 at 2:10
NolanNolan
9161 gold badge7 silver badges20 bronze badges
7
|
Show 2 more comments
3 Answers
Reset to default 13You do Array.findIndex in pure Javascript (ES6), without using any libs:
data.push(data.splice(data.findIndex(v => v.name == 'other'), 1)[0])
Array.findIndex is new, but most browsers people actually use these days have it. So you can probably use it. (Edge supports it, but not IE).
If you find the [0]
ugly, you can use spread to unpack the array (that way it'll also work for more than one item with some changes):
data.push(...data.splice(data.findIndex(v => v.name == 'other'), 1))
A plain (Vanilla) JavaScript will do just fine:
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
var data = [
{id:1,name:'apple'},
{id:2,name:'banana'},
{id:3,name:'other'},
{id:4,name:'tomato'},
{id:5,name:'strawberry'}
];
for(var x in data)data[x].name == "other" ? data.push( data.splice(x,1)[0] ) : 0;
console.log( data );
Use simple for loop
var data = [{
id: 1,
name: 'apple'
}, {
id: 2,
name: 'banana'
}, {
id: 3,
name: 'other'
}, {
id: 4,
name: 'tomato'
}, {
id: 5,
name: 'strawberry'
}];
// store the length of array
var len = data.length;
// iterate over all array elements
for (var i = 0; i < len; i++) {
// check value is `other`
if (data[i].name == 'other') {
// if value is other then move the object
data.push(data.splice(i, 1)[0]);
// decrement i since the element removed
i--;
// decrement len to avoid moved element
len--;
}
}
console.log(data);
data
array withvar copy = JSON.parse(JSON.stringify(data));
– seahorsepip Commented Jun 10, 2016 at 2:22