How would I compare the two to properly sort in order? - javascript

Objective is to distinguish the two arrays, and console log the products array in order by its price range, I tried using a nested for-loop to see if this can work out but hasn't gone off in the right track, was also thinking about using lo dash as well. How would I be able to compare the id's between the two and push them in order by price?
/*
Your task is to write a function or set of functions that console logs out the products ordered by price
*/
var products = [
{ id: 1, name: 'Buzzy' },
{ id: 2, name: 'Bytrex' },
{ id: 3, name: 'CactiDance' },
{ id: 4, name: 'CactiLoops' },
{ id: 5, name: 'Cadaver Jelly' },
{ id: 6, name: 'Caffeine Serene' },
{ id: 7, name: 'Cajun Sation' },
{ id: 8, name: 'Call it Green' },
{ id: 9, name: 'Callflex' },
{ id: 10, name: 'Calling Card Shark' },
{ id: 11, name: 'Calque' },
{ id: 12, name: 'Camel Meal Tea' },
{ id: 13, name: 'Camelot Chamomile' },
{ id: 14, name: 'Campxotica' },
{ id: 15, name: 'Camus the Killer Tale' },
{ id: 16, name: 'Candecor' },
{ id: 17, name: 'Candelarium' },
{ id: 18, name: 'CandID' },
{ id: 19, name: 'Candlelight Vittles' },
{ id: 20, name: 'Candy Ask' },
{ id: 21, name: 'Candy Floss' },
];
var prices = [
{ id: 6, price: 55 },
{ id: 14, price: 22 },
{ id: 15, price: 57 },
{ id: 4, price: 41 },
{ id: 18, price: 9 },
{ id: 17, price: 3 },
{ id: 2, price: 73 },
{ id: 7, price: 43 },
{ id: 5, price: 78 },
{ id: 1, price: 91 },
{ id: 8, price: 58 },
{ id: 16, price: 69 },
{ id: 13, price: 74 },
{ id: 19, price: 14 },
{ id: 21, price: 25 },
{ id: 12, price: 84 },
{ id: 20, price: 8 },
{ id: 9, price: 94 },
{ id: 10, price: 36 },
{ id: 3, price: 34 },
{ id: 11, price: 71 },
];

You can use map & findIndex. map will return a new array. Inside the callback function of map use findIndex and use it to find the index of the object where the id matches.
var products = [{
id: 1,
name: 'Buzzy'
},
{
id: 2,
name: 'Bytrex'
},
{
id: 3,
name: 'CactiDance'
},
{
id: 4,
name: 'CactiLoops'
},
{
id: 5,
name: 'Cadaver Jelly'
},
{
id: 6,
name: 'Caffeine Serene'
},
{
id: 7,
name: 'Cajun Sation'
},
{
id: 8,
name: 'Call it Green'
},
{
id: 9,
name: 'Callflex'
},
{
id: 10,
name: 'Calling Card Shark'
},
{
id: 11,
name: 'Calque'
},
{
id: 12,
name: 'Camel Meal Tea'
},
{
id: 13,
name: 'Camelot Chamomile'
},
{
id: 14,
name: 'Campxotica'
},
{
id: 15,
name: 'Camus the Killer Tale'
},
{
id: 16,
name: 'Candecor'
},
{
id: 17,
name: 'Candelarium'
},
{
id: 18,
name: 'CandID'
},
{
id: 19,
name: 'Candlelight Vittles'
},
{
id: 20,
name: 'Candy Ask'
},
{
id: 21,
name: 'Candy Floss'
},
];
var prices = [{
id: 6,
price: 55
},
{
id: 14,
price: 22
},
{
id: 15,
price: 57
},
{
id: 4,
price: 41
},
{
id: 18,
price: 9
},
{
id: 17,
price: 3
},
{
id: 2,
price: 73
},
{
id: 7,
price: 43
},
{
id: 5,
price: 78
},
{
id: 1,
price: 91
},
{
id: 8,
price: 58
},
{
id: 16,
price: 69
},
{
id: 13,
price: 74
},
{
id: 19,
price: 14
},
{
id: 21,
price: 25
},
{
id: 12,
price: 84
},
{
id: 20,
price: 8
},
{
id: 9,
price: 94
},
{
id: 10,
price: 36
},
{
id: 3,
price: 34
},
{
id: 11,
price: 71
},
];
let newArr = prices.map(function(item) {
let k = products.findIndex(elem => elem.id === item.id);
if (k !== -1) {
return products[k]
}
});
console.log(newArr)

You could sort the prices array first. Then loop through it and get the products for each id using find like this:
var products = [{id:1,name:"Buzzy"},{id:2,name:"Bytrex"},{id:3,name:"CactiDance"},{id:4,name:"CactiLoops"},{id:5,name:"Cadaver Jelly"},{id:6,name:"Caffeine Serene"},{id:7,name:"Cajun Sation"},{id:8,name:"Call it Green"},{id:9,name:"Callflex"},{id:10,name:"Calling Card Shark"},{id:11,name:"Calque"},{id:12,name:"Camel Meal Tea"},{id:13,name:"Camelot Chamomile"},{id:14,name:"Campxotica"},{id:15,name:"Camus the Killer Tale"},{id:16,name:"Candecor"},{id:17,name:"Candelarium"},{id:18,name:"CandID"},{id:19,name:"Candlelight Vittles"},{id:20,name:"Candy Ask"},{id:21,name:"Candy Floss"}],
prices = [{id:6,price:55},{id:14,price:22},{id:15,price:57},{id:4,price:41},{id:18,price:9},{id:17,price:3},{id:2,price:73},{id:7,price:43},{id:5,price:78},{id:1,price:91},{id:8,price:58},{id:16,price:69},{id:13,price:74},{id:19,price:14},{id:21,price:25},{id:12,price:84},{id:20,price:8},{id:9,price:94},{id:10,price:36},{id:3,price:34},{id:11,price:71}];
prices.sort((a, b) => a.price - b.price)
.forEach(p => {
console.log(products.find(a => a.id === p.id))
})

Related

find method not returning '0th' matched object react, or react-native or javascript

Not getting all matched objects, its is returning all objects except '{ ID:0, Name: 'General Group' }', i want all matched objects
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
]
const getMatchedobjects=()=> {
let result = [0,1,2,3,4,]
let matchedArray = arrDashboardIconGroups.filter(val =>result.find(
(items)=>items == val.ID))
console.log(matchedArray)
}
getMatchedobjects()
You are returning the value of
result.find((items) => items == val.ID)
In the first case, the value returned is 0 which is a falsy value. So It won't include in the final filter result.
You can run the below code and see the returning values.
let arrDashboardIconGroups = [
{ ID: 0, Name: "General Group" },
{ ID: 1, Name: "Patient Administration" },
{ ID: 2, Name: "Medical Charts" },
{ ID: 3, Name: "Medical Procedures" },
{ ID: 13, Name: "Purchase" },
{ ID: 14, Name: "Sales" },
{ ID: 5, Name: "Insurance" },
{ ID: 4, Name: "Cash" },
{ ID: 6, Name: "Pharmacy" },
{ ID: 7, Name: "Inventory" },
{ ID: 8, Name: "Lab" },
{ ID: 9, Name: "Imaging" },
{ ID: 10, Name: "In Patient" },
{ ID: 11, Name: "System Administration" },
{ ID: 12, Name: "Accounting" },
];
const getMatchedobjects = () => {
let result = [0, 1, 2, 3, 4];
let matchedArray = arrDashboardIconGroups.filter((val) => {
const returnResult = result.find((items) => items == val.ID);
console.log(returnResult);
return returnResult;
// result.includes(val.ID)
});
console.log(matchedArray);
};
getMatchedobjects();
Alternatively, you can use includes
let arrDashboardIconGroups = [
{ ID: 0, Name: "General Group" },
{ ID: 1, Name: "Patient Administration" },
{ ID: 2, Name: "Medical Charts" },
{ ID: 3, Name: "Medical Procedures" },
{ ID: 13, Name: "Purchase" },
{ ID: 14, Name: "Sales" },
{ ID: 5, Name: "Insurance" },
{ ID: 4, Name: "Cash" },
{ ID: 6, Name: "Pharmacy" },
{ ID: 7, Name: "Inventory" },
{ ID: 8, Name: "Lab" },
{ ID: 9, Name: "Imaging" },
{ ID: 10, Name: "In Patient" },
{ ID: 11, Name: "System Administration" },
{ ID: 12, Name: "Accounting" },
];
const getMatchedobjects = () => {
let result = [0, 1, 2, 3, 4];
let matchedArray = arrDashboardIconGroups.filter((val) => result.includes(val.ID));
console.log(matchedArray);
};
getMatchedobjects();
Issue
Find returns a value/object (the first matching), but it seems you want to filter the arrDashboardIconGroups array by those that match an id specified in the result array. When result[0] is returned, 0 is the value, which is falsey, and the filter doesn't return the element from the arrDashboardIconGroups array.
Solution
Use Array.prototype.some to return the boolean that filter needs to include an element in the result array.
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
];
const getMatchedobjects = () => {
let result = [0,1,2,3,4,];
let matchedArray = arrDashboardIconGroups.filter(val => result.some((items)=>items == val.ID));
console.log(matchedArray);
}
getMatchedobjects();
Alternatively you could also check that result array includes the matching id.
let arrDashboardIconGroups = [
{ ID:0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
];
const getMatchedobjects=()=> {
let result = [0,1,2,3,4,];
let matchedArray = arrDashboardIconGroups.filter(val => result.includes(val.ID)) ;
console.log(matchedArray);
}
getMatchedobjects();
find() returns value of the first element in the provided array that satisfies the provided testing function. Since 0 is a falsy value, it does not include it in the final array. See MDN Docs
You can map the result array, and use arrDashboardIconGroups's find function, and return the matching objects.
let arrDashboardIconGroups = [
{ ID: 0, Name: 'General Group' },
{ ID: 1, Name: 'Patient Administration' },
{ ID: 2, Name: 'Medical Charts' },
{ ID: 3, Name: 'Medical Procedures' },
{ ID: 13, Name: 'Purchase' },
{ ID: 14, Name: 'Sales' },
{ ID: 5, Name: 'Insurance' },
{ ID: 4, Name: 'Cash' },
{ ID: 6, Name: 'Pharmacy' },
{ ID: 7, Name: 'Inventory' },
{ ID: 8, Name: 'Lab' },
{ ID: 9, Name: 'Imaging' },
{ ID: 10, Name: 'In Patient' },
{ ID: 11, Name: 'System Administration' },
{ ID: 12, Name: 'Accounting' }
]
const getMatchedobjects=()=> {
let result = [0, 1,2,3,4,99] //99 for trial
let matchedArray = result.map((res)=>arrDashboardIconGroups.find((val)=>val.ID == res)).filter(Boolean);
console.log(matchedArray)
}
getMatchedobjects()

How to duplicate an object in an array by given quantity, ES6 and above

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

How to convert array of objects into custom grouped array

I am trying to convert the data object to custom format
This is my data which i want to convert
[
{ Name: 15, GroupID: 1, Id: 1 },
{ Name: 16, GroupID: 1, Id: 1 },
{ Name: 17, GroupID: 2, Id: 2 },
{ Name: 18, GroupID: 2, Id: 2 },
{ Name: 15, GroupID: 3, Id: 3 },
{ Name: 16, GroupID: 3, Id: 3 },
{ Name: 17, GroupID: 4, Id: 4 },
{ Name: 18, GroupID: 4, Id: 4 }
];
This is what i want to acheive
{ GroupID: 1 },
{ Name: 15, Id: 1 },
{ Name: 16, Id: 1 },
{ GroupID: 2 },
{ Name: 17, Id: 2 },
{ Name: 18, Id: 2 },
{ GroupID: 3 },
{ Name: 15, Id: 3 },
{ Name: 16, Id: 3 },
{ GroupID: 4 },
{ Name: 17, Id: 4 },
{ Name: 18, Id: 4 }
This is what i have tried till now
var data = [
{ Name: 15, GroupID: 1, Id: 1 },
{ Name: 16, GroupID: 1, Id: 1 },
{ Name: 17, GroupID: 2, Id: 2 },
{ Name: 18, GroupID: 2, Id: 2 },
{ Name: 15, GroupID: 3, Id: 3 },
{ Name: 16, GroupID: 3, Id: 3 },
{ Name: 17, GroupID: 4, Id: 4 },
{ Name: 18, GroupID: 4, Id: 4 }
];
var previousGroupId;
var newObject = new Object();
for (index in data) {
var groupId = data[index].GroupID;
if (groupId != previousGroupId) {
var newGroup = "GroupId" + groupId;
newObject[newGroup] = new Array();
for (index in data) {
if (data[index].GroupID == groupId) {
var customObject = {
"GroupID": groupId,
"Name": data[index].Name,
"Id": data[index].Id
};
newObject[newGroup].push(customObject);
}
}
}
previousGroupId = groupId;
}
console.log(newObject);
even i tried to refer this
Javascript group objects by property
any suggestions would be helpful.
Assuming an array of objects as result set, you could take a hash table with arrays and take a flat array as result set.
var data = [{ Name: 15, GroupID: 1, Id: 1 }, { Name: 16, GroupID: 1, Id: 1 }, { Name: 17, GroupID: 2, Id: 2 }, { Name: 18, GroupID: 2, Id: 2 }, { Name: 15, GroupID: 3, Id: 3 }, { Name: 16, GroupID: 3, Id: 3 }, { Name: 17, GroupID: 4, Id: 4 }, { Name: 18, GroupID: 4, Id: 4 }],
result = Object
.values(data.reduce((r, { Name, GroupID, Id }) => {
r[GroupID] = r[GroupID] || [{ GroupID }];
r[GroupID].push({ Name, Id });
return r;
}, {}))
.flat();
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to sort an array by frequency of occurrence?

I've array like this :
people = [
{id:1,name:'abc',age:18},
{id:2,name:'abcd',age:20},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:5,name:'abcg',age:21},
{id:6,name:'abch',age:20},
{id:7,name:'abci',age:17},
]
Now I want Sorting an array order by frequency of occurence(age) in JavaScript.
Output like this:
people = [
{id:1,name:'abc',age:18},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:2,name:'abcd',age:20},
{id:6,name:'abch',age:20},
{id:5,name:'abcg',age:21},
{id:7,name:'abci',age:17},
]
So how I can do it ?
I've tried follow answer of Nina Scholz but i've a bug like this.
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abcg', age: 21 }, { id: 8, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) => count[b.age] - count[a.age]);
console.log(people);
Output is :
people = [
{id:1,name:'abc',age:18},
{id:3,name:'abce',age:18},
{id:4,name:'abcf',age:18},
{id:2,name:'abcd',age:20},
{id:5,name:'abcg',age:21},
{id:6,name:'abch',age:20},
{id:5,name:'abcg',age:21},
{id:7,name:'abci',age:17},
]
Just count ages and sort descending by the count.
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) => count[b.age] - count[a.age]);
console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }
For sorting by count and age, add a logical OR || along with the delta of the ages.
var people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abcg', age: 21 }, { id: 8, name: 'abci', age: 17 }],
count = people.reduce((r, { age }) => (r[age] = (r[age] || 0) + 1, r), {});
people.sort((a, b) =>
count[b.age] - count[a.age] ||
b.age - a.age
);
console.log(people);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try this:
let people = [{ id: 1, name: 'abc', age: 18 }, { id: 2, name: 'abcd', age: 20 }, { id: 3, name: 'abce', age: 18 }, { id: 4, name: 'abcf', age: 18 }, { id: 5, name: 'abcg', age: 21 }, { id: 6, name: 'abch', age: 20 }, { id: 7, name: 'abci', age: 17 }];
let tmpAge = {};
for (let i = 0; i < people.length; i++) {
if (!tmpAge[people[i].age]) {
tmpAge[people[i].age] = 1;
} else {
tmpAge[people[i].age]++;
}
}
people.sort((a, b) => tmpAge[b.age] - tmpAge[a.age]);
console.log(people);
const people = [
{ id: 1, name: 'abc', age: 18 },
{ id: 2, name: 'abcd', age: 20 },
{ id: 3, name: 'abce', age: 18 },
{ id: 4, name: 'abcf', age: 18 },
{ id: 5, name: 'abcg', age: 21 },
{ id: 6, name: 'abch', age: 20 },
{ id: 7, name: 'abci', age: 17 },
];
const agesFrequency = people.reduce((previousValue, currentValue) => {
const { age } = currentValue;
return {
...previousValue,
[age]: (
previousValue[age] || 0
) + 1,
};
}, {});
people.sort((
(a, b) =>
agesFrequency[b.age] - agesFrequency[a.age]
));
console.log(people);
I solved it . Change function sort to
people.sort((a, b) => {
if (count[b.age] === count[a.age]) {
if (b.age < a.age) {
return -1;
}
if (b.age > a.age) {
return 1;
}
return 0;
} else if (count[b.age] < count[a.age]) {
return -1;
}
});
Posted on behalf of OP

Typescript - Rearrange specific object

I've arranged object in alphabetical order. I wanna move "Others" at last in dropdown. Is there any way to acheive this?
this.registrationMerchantService.getBusinessCategories().subscribe(response => {
this.businessCategories = response;
this.businessCategories.sort(function(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
});
Json objects
export const BUSINESS_CATEGORIES: BusinessCategory[] = [
{ id: 1, name: 'Food & Beverage' },
{ id: 2, name: 'Transportation & Travel' },
{ id: 3, name: 'Fashion' },
{ id: 4, name: 'Leisure' },
{ id: 5, name: 'Digital Content' },
{ id: 6, name: 'Health and Beauty' },
{ id: 7, name: 'Online Shopping' },
{ id: 8, name: 'Telecommunications' },
{ id: 9, name: 'Utilities' },
{ id: 10, name: 'Others' },
{ id: 11, name: 'General' },
{ id: 12, name: 'Insurance' },
{ id: 13, name: 'Services' },
{ id: 14, name: 'Appliances' },
{ id: 15, name: 'Entertainment' },
{ id: 16, name: 'Household Goods & Groceries' },
];
Current result:
Is there any idea to move "Others" at last in the dropdown list
You can try sorting the array filtering out "Others" and then add it at the end:
var categories = [
{ id: 1, name: 'Food & Beverage' },
{ id: 2, name: 'Transportation & Travel' },
{ id: 3, name: 'Fashion' },
{ id: 4, name: 'Leisure' },
{ id: 5, name: 'Digital Content' },
{ id: 6, name: 'Health and Beauty' },
{ id: 7, name: 'Online Shopping' },
{ id: 8, name: 'Telecommunications' },
{ id: 9, name: 'Utilities' },
{ id: 10, name: 'Others' },
{ id: 11, name: 'General' },
{ id: 12, name: 'Insurance' },
{ id: 13, name: 'Services' },
{ id: 14, name: 'Appliances' },
{ id: 15, name: 'Entertainment' },
{ id: 16, name: 'Household Goods & Groceries' },
];
const sorted = categories.sort(function(a, b) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
console.log(
sorted
.filter(x=>x.name!=="Others")//remove "Others"
.concat(sorted.find(x=>x.name==="Others"))//add "Others" at the end
);
You can create a function that accepts an ignore list, and you can return a sorting function that takes care of ignoring certain names, by placing them above or below in the list.
You can fiddle with the sorting direction to get the desired result.
var BUSINESS_CATEGORIES = [
{ id: 1, name: 'Food & Beverage' },
{ id: 2, name: 'Transportation & Travel' },
{ id: 3, name: 'Fashion' },
{ id: 4, name: 'Leisure' },
{ id: 5, name: 'Digital Content' },
{ id: 6, name: 'Health and Beauty' },
{ id: 7, name: 'Online Shopping' },
{ id: 8, name: 'Telecommunications' },
{ id: 9, name: 'Utilities' },
{ id: 10, name: 'Others' },
{ id: 11, name: 'General' },
{ id: 12, name: 'Insurance' },
{ id: 13, name: 'Services' },
{ id: 14, name: 'Appliances' },
{ id: 15, name: 'Entertainment' },
{ id: 16, name: 'Household Goods & Groceries' },
];
let categories = this.BUSINESS_CATEGORIES.sort(customSort([ 'Others' ], 1));
console.log(JSON.stringify(categories, null, 2));
function customSort(ignoreList, direction) {
ignoreList = ignoreList || [];
direction = direction || 1;
return function(a, b) {
var aPos = ignoreList.indexOf(a.name);
var bPos = ignoreList.indexOf(b.name);
if (bPos > -1) return -1 * direction;
if (aPos > -1) return 1 * direction;
return a.name.localeCompare(b.name) * direction;
}
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Categories

Resources