Sort array of objects by string following the order given by another array [duplicate]












0
















This question already has an answer here:




  • Sort an array of object by a property (with custom order, not alphabetically)

    3 answers



  • Order array by predefined rules

    4 answers




I have an array of objects with the _id index



[{
_id: 'zzz'
}, {
_id: 'abc'
}, {
_id: 'xxx'
}, {
_id: '123'
}, {
_id: 'several other objects not in the array'
}]


I also have an array like this



['abc', '123', 'xxx', 'zzz']


how can I make sure that the array of objects is correctly ordered by the array given?



I'm using 2 for loops but really looks ugly, and I'm sure there's a better way for this. thanks!










share|improve this question















marked as duplicate by Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

    – user3297291
    Nov 16 '18 at 8:29











  • elegant, thank you

    – sathia
    Nov 16 '18 at 8:29
















0
















This question already has an answer here:




  • Sort an array of object by a property (with custom order, not alphabetically)

    3 answers



  • Order array by predefined rules

    4 answers




I have an array of objects with the _id index



[{
_id: 'zzz'
}, {
_id: 'abc'
}, {
_id: 'xxx'
}, {
_id: '123'
}, {
_id: 'several other objects not in the array'
}]


I also have an array like this



['abc', '123', 'xxx', 'zzz']


how can I make sure that the array of objects is correctly ordered by the array given?



I'm using 2 for loops but really looks ugly, and I'm sure there's a better way for this. thanks!










share|improve this question















marked as duplicate by Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

    – user3297291
    Nov 16 '18 at 8:29











  • elegant, thank you

    – sathia
    Nov 16 '18 at 8:29














0












0








0









This question already has an answer here:




  • Sort an array of object by a property (with custom order, not alphabetically)

    3 answers



  • Order array by predefined rules

    4 answers




I have an array of objects with the _id index



[{
_id: 'zzz'
}, {
_id: 'abc'
}, {
_id: 'xxx'
}, {
_id: '123'
}, {
_id: 'several other objects not in the array'
}]


I also have an array like this



['abc', '123', 'xxx', 'zzz']


how can I make sure that the array of objects is correctly ordered by the array given?



I'm using 2 for loops but really looks ugly, and I'm sure there's a better way for this. thanks!










share|improve this question

















This question already has an answer here:




  • Sort an array of object by a property (with custom order, not alphabetically)

    3 answers



  • Order array by predefined rules

    4 answers




I have an array of objects with the _id index



[{
_id: 'zzz'
}, {
_id: 'abc'
}, {
_id: 'xxx'
}, {
_id: '123'
}, {
_id: 'several other objects not in the array'
}]


I also have an array like this



['abc', '123', 'xxx', 'zzz']


how can I make sure that the array of objects is correctly ordered by the array given?



I'm using 2 for loops but really looks ugly, and I'm sure there's a better way for this. thanks!





This question already has an answer here:




  • Sort an array of object by a property (with custom order, not alphabetically)

    3 answers



  • Order array by predefined rules

    4 answers








javascript arrays node.js object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:41









Krupesh Kotecha

2,07311136




2,07311136










asked Nov 16 '18 at 8:25









sathiasathia

1,59921637




1,59921637




marked as duplicate by Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Nina Scholz arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

    – user3297291
    Nov 16 '18 at 8:29











  • elegant, thank you

    – sathia
    Nov 16 '18 at 8:29














  • 2





    Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

    – user3297291
    Nov 16 '18 at 8:29











  • elegant, thank you

    – sathia
    Nov 16 '18 at 8:29








2




2





Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

– user3297291
Nov 16 '18 at 8:29





Another similar question that's useful: stackoverflow.com/questions/37671695/… (also answered by Nina Scholz 😉)

– user3297291
Nov 16 '18 at 8:29













elegant, thank you

– sathia
Nov 16 '18 at 8:29





elegant, thank you

– sathia
Nov 16 '18 at 8:29












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

List item for chat from Array inside array React Native

Xamarin.iOS Cant Deploy on Iphone

App crashed after uploaded to heroku server