New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added key:value notation support to getShallowProperty() #90
Conversation
… get a specific array-entry identified by key:value pair
- commented out console.log messages
object-path-key-value
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 @utillity<https://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 GitHub<#90 (comment)>, or mute the thread<https://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). // 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.... |
added key:value notation support to getShallowProperty(), allowing to get a specific array-entry identified by key:value pair