Practising Ramda again.
So the situation is I have an obj:
const originalObj = {
foo: "bar",
std: "min",
baz: "",
key1: undefined,
key2: "exit",
key3: "val3",
key4: "",
};
And I have an array that I know beforehand:
const toCheckArray = ["baz", "key1", "key2", "key3", "key4", "key5"];
For every element in the array, I need to check whether such element (as key) exists in the obj, and whether the corresponding value is nil/empty. If such key exists, and the value is non-zero/empty, then I make an HTTP call like this to update the value:
const findKey2AndUpdateObj = async (originalObj) => {
const originalKey2 = originalObj.key2;
const key2 = await remoteHttpCall(originalKey2);
return { ...originalObj, key2: key2 };
};
For all the elements in the array, the remote HTTP call would be exactly the same, apart from the payload, of course.
My way of thing is to filter the list first, by doing the following steps:
const hasArray = filter(has(__, originalObj), toCheckArray);this I believe will check whether the element as a prop exists in the target obj;- I am trying to apply
complement(anyPass([isNil, isEmpty]))to all the values of the obj and then somehow filter the corresponding key in the array; - Iterate the array? to make API calls and then update the obj.
I guess what I am thinking is not the best way of doing it. Would love to hear your ideas! Also memorising the API call would be amazing too!
Or maybe I should flip step 1 and step 2? Filter out all the nil/empty ones from the obj and then do the has check.
I ended up doing this: filter(has(__, reject(anyPass([isEmpty, isNil]))(obj)), __)(arr). But surely there is better way.
Cheers!