I am trying to create a sorting function that sorts a nested array of objects while giving a key dynamically (with different depths).
sortByKey(array, key){
var splitKey = key.split(".");
if(splitKey.length = 2){
return array.sort(function(a, b) {
var x = a[splitKey[0]][splitKey[1]]; var y = b[splitKey[0]][splitKey[1]];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
} else {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
}
I want to get rid of the if - else and use a for loop instead. The goal is that the function works with 'name', 'name.first' and 'name.first.another' (as an example). Is there a way to do this dynamically?
In other words, I want to use the same function with different arrays. So with one array I want to sort it calling sortByKey(array1, 'name') and with another sortByKey(array2, 'location.address') and maybe with a third sortByKey(array3, 'location.address.postalcode') or something like that.
Extract property extracting function
function prop(key) {
var keys = key.split('.');
return keys.reduce.bind(keys, function(obj, name) {
return obj[name]
})
}
and use it to well extract values :)
sortByKey(array, key){
var getKey = prop(key);
return array.sort(function(a, b){
var x = getKey(a); var y = getKey(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
})
}
I think you mean something like this:
function sortByKey(array, key){
var splitKey = key.split(".");
return array.sort(function(a, b) {
var ta = a;
var tb = b;
for (var i=0; i<splitKey.length; i++) {
ta = ta[splitKey[i]];
};
/// return ((a < b) ? -1 : ((a > b) ? 1 : 0)); // Too complex ;-)
return a - b;
});
};
Your problem is a misused assignment, where it should be a comparison.
if (splitKey.length === 2) {
// ^^^
A shorter approach could use Array#reduce.
function sortByKey(array, key) {
var getValue = function (o, k) { return o[k]; },
keys = key.split(".");
return array.sort(function (a, b) {
return keys.reduce(getValue, a) - keys.reduce(getValue, b);
});
}
var array = [{ a: 5, b: { c: 2 } }, { a: 7, b: { c: 1 } }, { a: 1, b: { c: 3 } }];
sortByKey(array, 'a');
console.log(array);
sortByKey(array, 'b.c');
console.log(array);
ES6
function sortByKey(array, key) {
const getValue =
(keys => object => keys.reduce((o, k) => o[k], object))
(key.split('.'));
return array.sort((a, b) => getValue(a) - getValue(b));
}
var array = [{ a: 5, b: { c: 2 } }, { a: 7, b: { c: 1 } }, { a: 1, b: { c: 3 } }];
sortByKey(array, 'a');
console.log(array);
sortByKey(array, 'b.c');
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
I have 2 objects a and b which both contains arrays.
I want to Merge the arrays present in each object and form a new object including the arrays and text nodes.
my code :-
var a = {
name1:[1,2],
name2:[3],
name3:'alisha',
name4:'japan'
};
var b = {
name1:[4],
name2:[5,6],
name3:'hello alisha!!'
};
newobject = function (obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
};
console.log(newobject(a,b))
What I am trying to do :-
I have 2 object a & b which may contain different data as well as similar data. I want to mix the data from the arrays if both objects have the same property, If the property of object is a string i want to update it with **b ** objects same property.
Output may not be accurate but It gives 100% hints to solve this problem
Output I am trying to get:-
{
name1:[1,2,4],
name2:[3,5,6],
name3:'hello alisha!',
name4:'japan'
}
Please don't use jquery
If it's literally the example above that you'd like to merge, then the following will do that for you.
I don't know if it matters in your case, but you may also want to consider handling instances where a.name1 is a string and b.name1 is an array, etc.
var a = {
name1: [1, 2],
name2: [3],
name3: 'alisha',
name4: 'japan'
};
var b = {
name1: [4],
name2: [5, 6],
name3: 'hello alisha!!'
};
function merge(a, b) {
var c = {};
for (var key in a) {
c[key] = a[key];
}
for (var key in b) {
c[key] = (Array.isArray(a[key])) ? a[key].concat(b[key]) : b[key];
}
return c;
}
console.log(merge(a, b));
You may easily extend to check for other types as well, like object etc.
var a = {
name1:[1,2],
name2:[3],
name3:'alisha',
name4:'japan'
};
var b = {
name1:[4],
name2:[5,6],
name3:'hello alisha!!',
name5:'new name'
};
function merge(a, b) {
var c = Object.keys(a).reduce(function(acc, key) {
if(!(key in b)) {
acc[key] = a[key]
} else if(Array.isArray(a[key]) && Array.isArray(b[key])) {
acc[key] = a[key].concat(b[key])
} else {
acc[key] = b[key]
}
return acc
}, {})
c = Object.keys(b).reduce(function(acc, key) {
if(!(key in acc)) {
acc[key] = b[key]
}
return acc
}, c)
return c
}
var d = merge(a, b)
var a = {
name1: [1, 2],
name2: [3],
name3: 'alisha',
name4: 'japan'
};
var b = {
name1: [4],
name2: [5, 6],
name3: 'hello alisha!!'
};
function mergeObjects(a, b) {
for (var key in b) {
a[key] = (Array.isArray(a[key])) ? a[key].concat(b[key]) : b[key];
}
return a;
}
console.log(mergeObjects(a, b));
I have a deeply nested object:
{ a: { b: { c: 3 }, d: 4 } }.
How to get all namespaces within this object?
So, I need to get:
['a.b.c', 'a.d'].
You can create recursive function using for...in loop.
var obj = {a: {b: {c: 3} }, d: 4 }
function getKeys(data, prev) {
var result = []
for (var i in data) {
var dot = prev.length ? '.' : '';
if (typeof data[i] == 'object') result.push(...getKeys(data[i], prev + dot + i))
else result.push(prev + dot + i)
}
return result;
}
console.log(getKeys(obj, ''))
Instead of for...in loop you can use Object.keys() and reduce().
var obj = {a: {b: {c: 3} }, d: 4 }
function getKeys(data, prev) {
return Object.keys(data).reduce(function(r, e) {
var dot = prev.length ? '.' : '';
if (typeof data[e] == 'object') r.push(...getKeys(data[e], prev + dot + e))
else r.push(prev + dot + e)
return r;
}, [])
}
console.log(getKeys(obj, ''))
var t = {a: {b: {c: 3} }, d: 4 };
path (t, '');
function path(t, sofar) {
if (Object.keys(t).length === 0)
console.log(sofar.substr(1));
var keys = Object.keys(t);
for (var i = 0 ; i < keys.length ; ++i) {
path(t[keys[i]], sofar+'.'+keys[i]);
}
}
You could create a script in order to flatten the object and return the keys. You could also think to convert it to an array and use the default flatten of arrays. Here an example of flattening the object.
var flattenObject = function(ob) {
var toReturn = {};
for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;
if ((typeof ob[i]) == 'object') {
var flatObject = flattenObject(ob[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
};
var obj = {a: {b: {c: 3} }, d: 4 }
console.log(Object.keys(flattenObject(obj))); // ['a.b.c', 'd']
p.s. your object in the question has a mistake, or what you want is not what you are asking. d is at the same level of a, so you can't achieve "a.d", but "d"
You could check the keys and iterate otherwise push the path to the result set.
function getKeys(object) {
function iter(o, p) {
var keys = Object.keys(o);
keys.length ?
keys.forEach(function (k) { iter(o[k], p.concat(k)); }):
result.push(p.join('.'));
}
var result = [];
iter(object, []);
return result;
}
var object = { a: { b: { c: 3 } }, d: 4 };
console.log(getKeys(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I am using this function to sort an array based on object key:
function keysrt(arr, key, reverse) {
var sortOrder = 1;
if(reverse){
sortOrder = -1;
}
return arr.sort(function(a, b) {
var x = a[key],
y = b[key];
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
It works well with this type of array, where key is on the first level:
var a = [
{ id: 0, last: 'Anne'},
{ id: 1, last: 'Odine'},
{ id: 2, last: 'Caroline'}
]
keysrt(a, 'last');
How can I make it work with this example, where title key is nested?
var b = [
{ id: 0, last: 'Anne', data:{title: 'habc'}},
{ id: 1, last: 'Odine', data:{title: 'asdf'}},
{ id: 2, last: 'Prentice', data:{title: 'tzuio'}}
]
keysrt(b, 'title');
For this idea the "key" variable changes into an array of keys: Then you specify the "path" to the nested value you want to sort on.
function keysrt(arr, keyArr, reverse) {
var sortOrder = 1;
if(reverse)sortOrder = -1;
return arr.sort(function(a, b) {
var x=a,y=b;
for (var i=0; i < keyArr.length; i++) {
x = x[keyArr[i]];
y = y[keyArr[i]];
}
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
keysrt(b,['data','title']);
If you are ready to change the function signature and the function call, here is a simple solution-
function keysrt(arr, prop, key, reverse) {
var sortOrder = 1;
if(reverse)sortOrder = -1;
return arr.sort(function(a, b) {
var x = a[prop][key]; var y = b[prop][key];
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
var b = [
{ id: 0, last: 'Anne', data:{title: 'habc'}},
{ id: 1, last: 'Odine', data:{title: 'asdf'}},
{ id: 2, last: 'Prentice', data:{title: 'tzuio'}}
]
keysrt(b,'data', 'title');
Here, prop represents the outer object, key would represent the nested key.
So, var y = b[prop][key] would basically mean you are accessing b.data.title
Hope it helps :) Happy coding!
If you need to make it generic, I think you can pass in a function that will retrieve the value from array item for comparison:
function keysrt(arr, reverse, getValueFn) {
var sortOrder = 1;
if(reverse)sortOrder = -1;
return arr.sort(function(a, b) {
var x = getValueFn(a); var y = getValueFn(b);
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
So that you can use it like:
keysrt(b, true, function(a){return a.data.title})
You can get working example with following code:
function keysrt(arr, key, reverse) {
var sortOrder = reverse ? -1 : 1;
return arr.sort(function(a, b) {
var x,y;
if(typeof a[key] !== "undefined") {
x = a[key];
y = b[key];
} else {
for(var prop in a) {
if(a[prop][key] !== "undefined") {
x = a[prop][key];
y = b[prop][key];
}
}
}
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
but I would propose more generic solution
function keysrt(arr, path, reverse) {
var sortOrder = reverse ? -1 : 1;
var pathSplitted = path.split(".");
if(arr.length <= 1) {
return arr;
}
return arr.sort(function(a, b) {
var x = a;
var y = b;
pathSplitted.forEach(function(key) {
x = x[key];
y = y[key];
});
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
in which one can provide a path to sorting field like this
var sorted = keysrt(b, 'data.title');
Demo: http://jsbin.com/cosugawoga/edit?js,console
To find a nested property value, any number of levels down, you can use JSON.stringify as a way to walk the object:
function get_nested_value(obj, prop) {
var result;
JSON.stringify(obj, function(key, value) {
if (key === prop) result = value;
});
return result;
}
Now:
function keysrt(arr, key, reverse) {
var sortOrder = 1;
if(reverse){
sortOrder = -1;
}
return arr.sort(function(a, b) {
var x = get_nested_value(a, key);
y = get_nested_value(b, key);
return sortOrder * ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
I have a function which sorts by name currently and an array of value / key pairs.
I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so:
var arr = [{name:'bob', artist:'rudy'},
{name:'johhny', artist:'drusko'},
{name:'tiff', artist:'needell'},
{name:'top', artist:'gear'}];
sort(arr, 'name'); //trying to sort by name
sort(arr, 'artist'); //trying to sort by artist
function sort(arr) {
arr.sort(function(a, b) {
var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
if (nameA < nameB) //sort string ascending
return -1;
if (nameA > nameB)
return 1;
return 0; //default return value (no sorting)
});
}
Array.prototype.sortOn = function(key){
this.sort(function(a, b){
if(a[key] < b[key]){
return -1;
}else if(a[key] > b[key]){
return 1;
}
return 0;
});
}
var arr = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];
arr.sortOn("name");
arr.sortOn("artist");
[edit 2020/08/14] This was rather an old answer and not very good as well, so simplified and revised.
Create a function that returns the sorting lambda (the Array.prototype.sort callback that does the actual sorting). That function can receive the key name, the kind of sorting (string (case sensitive or not) or numeric) and the sorting order (ascending/descending). The lambda uses the parameter values (closure) to determine how to sort.
const log = (...strs) =>
document.querySelector("pre").textContent += `\n${strs.join("\n")}`;
const showSortedValues = (arr, key) =>
` => ${arr.reduce((acc, val) => ([...acc, val[key]]), [])}`;
// the actual sort lamda factory function
const sortOnKey = (key, string, desc) => {
const caseInsensitive = string && string === "CI";
return (a, b) => {
a = caseInsensitive ? a[key].toLowerCase() : a[key];
b = caseInsensitive ? b[key].toLowerCase() : b[key];
if (string) {
return desc ? b.localeCompare(a) : a.localeCompare(b);
}
return desc ? b - a : a - b;
}
};
// a few examples
const onNameStringAscendingCaseSensitive =
getTestArray().sort( sortOnKey("name", true) );
const onNameStringAscendingCaseInsensitive =
getTestArray().sort( sortOnKey("name", "CI", true) );
const onValueNumericDescending =
getTestArray().sort( sortOnKey("value", false, true) );
// examples
log(`*key = name, string ascending case sensitive`,
showSortedValues(onNameStringAscendingCaseSensitive, "name")
);
log(`\n*key = name, string descending case insensitive`,
showSortedValues(onNameStringAscendingCaseInsensitive, "name")
);
log(`\n*key = value, numeric desc`,
showSortedValues(onValueNumericDescending, "value")
);
function getTestArray() {
return [{
name: 'Bob',
artist: 'Rudy',
value: 23,
}, {
name: 'John',
artist: 'Drusko',
value: 123,
}, {
name: 'Tiff',
artist: 'Needell',
value: 1123,
}, {
name: 'Top',
artist: 'Gear',
value: 11123,
}, {
name: 'john',
artist: 'Johanson',
value: 12,
}, ];
}
<pre></pre>
function keysrt(key) {
return function(a,b){
if (a[key] > b[key]) return 1;
if (a[key] < b[key]) return -1;
return 0;
}
}
someArrayOfObjects.sort(keysrt('text'));
Make your life easy and use a closure
https://stackoverflow.com/a/31846142/1001405
You can see the working example here
var filter = 'name', //sort by name
data = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];;
var compare = function (filter) {
return function (a,b) { //closure
var a = a[filter],
b = b[filter];
if (a < b) {
return -1;
}else if (a > b) {
return 1;
} else {
return 0;
}
};
};
filter = compare(filter); //set filter
console.log(data.sort(filter));
Looking at all the answers, I came up with my own solution that works cross-browser. The accepted solution does not work in IE or Safari. Also, the other solutions do not allow for sorting by descending.
/*! FUNCTION: ARRAY.KEYSORT(); **/
Array.prototype.keySort = function(key, desc){
this.sort(function(a, b) {
var result = desc ? (a[key] < b[key]) : (a[key] > b[key]);
return result ? 1 : -1;
});
return this;
}
var arr = [{name:'bob', artist:'rudy'}, {name:'johhny', artist:'drusko'}, {name:'tiff', artist:'needell'}, {name:'top', artist:'gear'}];
arr.keySort('artist');
arr.keySort('artist', true);
I have an nested object that I want to update it with values provided by object that contains similar structure but only the properties that I want updated. Creating a new result instead of modifying the initial objects is great too.
var initial =
{
a: 1,
b : {
c : 2,
d : 3
},
f: 5
};
var update = {
a: 2,
b: {
d: 2
}
};
function updateFunction (a,b) { return a+b;};
var result=
{
a: 3, // updateFunction (1,2)=> 3
b : {
c : 2,
d :5 // updateFunction (3,2) => 5
},
f: 5
};
Have not tested fully, but maybe,
assuming objects are simple as stated,
function updateFunction (a,b) { return a + b;};
function recurse(initial, update){
for(prop in initial){
if({}.hasOwnProperty.call(initial, prop) && {}.hasOwnProperty.call(update, prop)){
if(typeof initial[prop] === 'object' && typeof update[prop] === 'object'){
recurse(initial[prop], update[prop]);
}
else{
initial[prop] = updateFunction(initial[prop], update[prop]);
}
}
}
}
recurse(initial, update);
EDIT
If result is expected without changing initial
function updateFunction (a,b) { return a + b;};
function recurse(initial, update){
var result = {};
for(prop in initial){
if({}.hasOwnProperty.call(initial, prop)){
result[prop] = initial[prop];
if({}.hasOwnProperty.call(update, prop)){
if(typeof initial[prop] === 'object' && typeof update[prop] === 'object'){
result[prop] = recurse(initial[prop], update[prop]);
}
else{
result[prop] = updateFunction(initial[prop], update[prop]);
}
}
}
}
return result;
}
var result = recurse(initial, update);
hope this helps.
Here's how I'd do it:
// The parallel to Array.map
Object.map = function(obj, f) {
var result = {};
for(k in obj)
if({}.hasOwnProperty.call(obj, k))
result[k] = f(k, obj[k]);
return result;
}
// Takes two objects and uses `resolve` to merge them
function merge(a, b, resolve) {
return Object.map(a, function(k, a_value) {
if(k in b)
return resolve(a_value, b[k]);
else
return a_value;
});
}
// same as above, but recursing when an object is found
function recursive_merge(a, b, resolve) {
return merge(a, b, function(a_value, b_value) {
if(typeof a_value == 'object' && typeof b_value == 'object')
return recursive_merge(a_value, b_value, resolve);
else
return resolve(a_value, b_value);
});
}
result = recursive_merge(initial, update, function(a, b) { return a + b; })