Closed utillity closed 7 years ago
Hi @utillity , great the proactivity, thanks! However, this feature seems addressing a quite unusual requirement and it introduces more complexity into the code, so I cannot include it into the main library.
No prob, just wanted to share the code. I do believe there should be an easier way to traverse arrays, apart from their index (which can change without notice), though. Tilli
From: Mario Casciaro notifications@github.com Reply-To: mariocasciaro/object-path reply@reply.github.com Date: Thursday, 26 October 2017 at 17:46 To: mariocasciaro/object-path object-path@noreply.github.com Cc: Tilfried Weissenberger tilli@weissenberger.at, Mention mention@noreply.github.com Subject: Re: [mariocasciaro/object-path] added key:value notation support to getShallowProperty() (#90)
Hi @utillityhttps://github.com/utillity , great the proactivity, thanks! However, this feature seems addressing a quite unusual requirement and it introduces more complexity into the code, so I cannot include it into the main library.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/mariocasciaro/object-path/pull/90#issuecomment-339710003, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AFSu9pB2DUszzsymXFusIywzCJGkOZQpks5swKlKgaJpZM4PrEVq.
I wanted something like this as well. The way I did it was to add a find/search function. This way the main functions can stay simple (one of the big reasons I picked this to work with). See mine below. It can search an array or an object and return any element that can resolve a "has" against "path" if there is no "value", or a "get" against "value". If "value" is an array it will check to see if any of the values in "value" match. If "index" is true it will return the elements indexs/keys instead. And "all" indicates whether to return all matches or just the first one it finds.
// Doesnt take into account inherited properties like object-path does
// I was only interested in enumarable properties, so it uses Object.keys
function find(obj, what, all, index, value) {
let result = [];
const isArray = Array.isArray(obj);
const keys = isArray ? undefined : Object.keys(obj);
for (let i = 0, end = isArray ? obj.length : keys.length; i < end; i++) {
if (!all && result.length) return result[0];
const element = obj[isArray ? i : keys[i]];
if (objectPath.has(element, what)) {
if (arguments.length == 4) {
result.push(index ? isArray ? i : keys[i] : element);
continue;
}
let foundValue = objectPath.get(element, what);
if (!Array.isArray(value)) {
if (foundValue === value) result.push(index ? isArray ? i : keys[i] : element);
continue;
} else for (let j = 0; j < value.length; j++) {
if (value[j] == foundValue) result.push(index ? isArray ? i : keys[i] : element);
if (!all && result.length) return result[0];
}
}
}
return result
}
I used this in my EmitterObject.... https://gist.github.com/PAEz/b6fc1687e4e963796f189d539d8d9d0c ... still working on it, dont judge me ;P
added key:value notation support to getShallowProperty(), allowing to get a specific array-entry identified by key:value pair