I have an array of objects below. This is the raw data
const data = [
{id:1, sItem:"This is javascript", status:"trending"},
{id:2, sItem:"javascript is fun", status:"trending"},
{id:3, sItem:"learning javascript", status:"trending"},
{id:4, sItem:"how to code in javascript", status:"trending"},
{id:5, sItem:"javascript will rule", status:"trending"},
{id:6, sItem:"javascript can do anything", status:"trending"}
]
And I want to search the sItem properties with the keyword javascript and reorder them so that where ever javascript comes at the start of sItem that element should come first with index 0. Other objects starting with javascript should come next followed by the rest of the array objects.
expected o/p
const data = [
{id:2, sItem:"javascript is fun", status:"trending"},
{id:5, sItem:"javascript will rule", status:"trending"},
{id:6, sItem:"javascript can do anything", status:"trending"},
{id:1, sItem:"This is javascript", status:"trending"},
{id:3, sItem:"learning javascript", status:"trending"},
{id:4, sItem:"how to code in javascript", status:"trending"}
]
I have tried to filter array objects
const search = "javascript" data.filter(obj => Object.values(obj).some(val => val.includes(search)))
any suggestion will be useful.
CodePudding user response:
This will separate out the 'javascript' ones from the rest, then alphabetically sort them all separately. Finally it combines it all into one new array.
[...data.reduce((b,a) => {
if (a.sItem.split(" ")[0].toLowerCase() == "javascript") b[0].push(a);
else b[1].push(a);
return b
}, [[],[]]).map(m => m.sort((a,b) => a.sItem.localeCompare(b.sItem)))]
The [...data
will take the output and line it up as a single array. The reduce(b,a
will separate out the javascript ones from the rest. The .sort(a,b
will aphabetically order each of the 2 arrays (the ones with javascript as a first string and the rest.
const data = [{id:1, sItem:"This is javascript", status:"trending"},{id:2, sItem:"javascript is fun", status:"trending"},{id:3, sItem:"learning javascript", status:"trending"},{id:4, sItem:"how to code in javascript", status:"trending"},{id:5, sItem:"javascript will rule", status:"trending"},{id:6, sItem:"javascript can do anything", status:"trending"}]
let neworder = [...data.reduce((b,a) => {
if (a.sItem.split(" ")[0].toLowerCase() == "javascript") b[0].push(a);
else b[1].push(a);
return b
}, [[],[]]).map(m => m.sort((a,b) => a.sItem.localeCompare(b.sItem)))]
console.log(neworder)
CodePudding user response:
You can try this, loop over the items, validate if the string matches the term, and save into two arrays, one contains the strings that starts with the match and others only contains it, finally concat them to create the sorted arr.
const filterByKeyword = (arr, keyword) => {
const [a, b] = [[], []];
for (let i = 0; i < arr.length; i ) {
const el = arr[i];
if (el.sItem.includes(keyword)) {
if (el.sItem.startsWith(keyword)) a.push(arr[i]);
else b.push(arr[i]);
}
}
return [...a, ...b];
};
console.log(filterByKeyword(data, search));
CodePudding user response:
If this question is not very specific to JavaScript logic then may be u can use this light weight but fast npm package like ss-search to perform your search.
var ssSearch = require("ss-search")
const data = [{id:2, sItem:"javascript is fun", status:"trending"},{id:5, sItem:"javascript will rule", status:"trending"},{id:6, sItem:"javascript can do anything", status:"trending"},{id:1, sItem:"This is javascript", status:"trending"},{id:3, sItem:"learning javascript", status:"trending"},{id:4, sItem:"how to code in javascript", status:"trending"}]
const searchKeys = ["sItem"]
const searchText = "javascript"
const results = ssSearch.search(data, searchKeys, searchText)
console.log(results)
You may try the above code in npm.runkit