Convert String to Object Key Name - javascript

I'm sending different objects to a component outside, and component data varies depending on objects. I'm getting the names with the Object.key function because the keywords I send have different key. Then I want to sort by the key. For this I need to define the name I received with Object.key function. How can I do it?
upSortTable(items, val) {
//items = Object,
//val = index
let Keys = Object.keys(items[0]); // ["item_id","item_title"]
let keyname = Keys[val]; //item_id String value
//want to use in sort function as b.item_id
return items.sort(function(a, b) {
return b.keyname - a.keyname;
});
},

You'll need to use computed property:
return items.sort(function(a, b) {
return b[keyname] - a[keyname];
});
When you do a.keyname you're actually looking for the property keyname in a itself.

Related

returning a value if a String includes the key

I have a JS Object Literal (logGroupMap) containing alarm names as keys, and their log groups as values. Alarm names are passed in with consistent names of varying length but with generated characters at the end each time, so I'm trying to see if the string includes a key from the logGroupMap object literal, and if so I want to get the value associated with that key.
let logGroup = (alarmName) => {
Object.keys(logGroupMap).forEach((key) => {
if (alarmName.includes(key)) { logGroup = logGroupMap.key; }
});
};
console.log(logGroup(messageDetails.AlarmName));
Currently this is returning undefined in all cases.
Use the Array.find() method to search for a matching key.
To access a property dynamically, use logGroupMap[key]. logGroupMap.key returns the value of the property named key, it doesn't use key as a variable.
function logGroup(alarmName) {
let found = Object.keys(logGroupMap).find((key, value) => alarmName.includes(key));
if (found) {
return logGroupMap[key];
}
}

How to compare dynamic string with static string in object with regex javascript

Let say I have one object with key as string which consists dynamic values-
let obj = {
"/prodp/v1/engagemnt/{engID}/doc/{docID}":"roles_1",
"/prodp/v1/engagemnt/{engID}/review/{reviewID}": "roles_2"
}
I want to compare and find value for below string from above object.
/prodp/v1/engagemnt/1234/doc/doc_1234
So as per requirement above string should return roles_1 value from object.
Can anyone suggest the solution for this issue?
you can use Object.keys(obj) to get all keys of an object as an array of string.
const obj = {
"/prodp/v1/engagemnt/{engID}/doc/{docID}":"roles_1",
"/prodp/v1/engagemnt/{engID}/review/{reviewID}": "roles_2"
}
let roles;
Object.keys(obj).forEach((key) => {
// key will be equal to "/prodp/v1/engagemnt/{engID}/doc/{docID}":"roles_1" then it will be equal to the next key of the object
roles = // do your regex here for whatever you want to check
});

ES6 / Lodash, how to check if an object contains a specific value of given request?

With having an object like below, how would I know that a certain value, e.g: template:d51a08fe-fb60-4da0-841b-03a732f19576, existed in this object?
const obj= {
"5871": "template:d51a08fe-fb60-4da0-841b-03a732f19576",
"6993": "template:d07479ff-1172-4464-996d-32d6c7358979",
"5123": "template:280ac289-c726-4cb1-8a11-9ae1c987b399"
};
I was trying to use lodash some but this will need the property name, but I cannot provide the property here such as 5871
You can use Object.values() to get a list of all values in your Object. This will return an array of all values. Then you simply use includes function on the array to see if that values exists in the array.
const obj= {
"5871": "template:d51a08fe-fb60-4da0-841b-03a732f19576",
"6993": "template:d07479ff-1172-4464-996d-32d6c7358979",
"5123": "template:280ac289-c726-4cb1-8a11-9ae1c987b399"
};
const valueExists = Object.values(obj).includes("template:d07479ff-1172-4464-996d-32d6c7358979")
console.log(valueExists)
Lodash's _.includes() works with objects as well as arrays. In addition, you can use _.findKey() to, well, find the key:
const obj = {
"5871": "template:d51a08fe-fb60-4da0-841b-03a732f19576",
"6993": "template:d07479ff-1172-4464-996d-32d6c7358979",
"5123": "template:280ac289-c726-4cb1-8a11-9ae1c987b399"
};
const exists = _.includes(obj, 'template:d07479ff-1172-4464-996d-32d6c7358979')
const key = _.findKey(obj, v => v === 'template:d07479ff-1172-4464-996d-32d6c7358979')
console.log({ exists, key })
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

How to filter the dynamic Json data using underscore.js?

I have an array of json objects with dynamic data in it i.e both the keys and values are dynamic as :
arr=[{"a":"email"},{"b":"chat"},{"c":"email"},{"d":"chat"},{"e":"email"}]
The size of arr may change i.e it may have any number of json objects.
How do I filter the data using underscore.js ??
This is what I was trying to do :
filterData(searchValue:string){
this.skillGrpsFilteredData= _.filter(this.skillGroupsAndTypes, function(obj) {
return ~obj.toLowerCase().includes(searchValue) as any;
});
}
But this approach is not working as the keys in the above array obj is dynamic i.e "a,b,c,d,e,.." are dynamic in nature.
And searchValue is the value that comes from the frontend UI.
How do I perform search using keys i.e if I type searchValue=a it should give me that object and so on .
Thanks & Regards
Try the following function. In the array parameter you can pass arr=[{"a":"email"},{"b":"chat"},{"c":"email"},{"d":"chat"},{"e":"email"}] and in searchKey parameter pass such as "a" it will return {"a":"email"}. If match not found function will return null
searchByKey(array,searchKey ) {
for (let obj of array) {
for (let key in obj) {
if (key == searchKey) {
return obj;
}
}
}
return null;
}
Try this
Following function filters array objects containing the search value both in key and value of the object
in your case you can pass 'a' or "email" to get the objects
function searchObject(array , searchValue){
return array.filter(item=>{ return Object.keys(item).includes(searchValue) ||Object.values(item).includes(searchValue)})
}
let arr=[{"a":"email"},{"b":"chat"},{"c":"email"},{"d":"chat"},{"e":"email"}]
console.log(searchObject(arr,'a'));
if you want just to filter by key and not by value of the object remove the
Object.values(item).includes(searchValue)
from the or condition
if you want to try indexOf
try
function searchObject(array , searchValue){
return array.filter(item=>{ return
Object.keys(item).indexOf(searchValue)>=0})
}

how to insert new object in node js array if key not exist

I want to create data structure like that.
Var ans =[{"b":[1,2]},{"g":[100,2]}]
I want to create a new object within list if key not exists in list ans.
Else if key exists in one object of ans list then I want to add new values into the object of ans list
For Example:
Example 1) new data c:{2000}
then
Var ans =[{"b":[1,2]},{"g":[100,2]},{c:[2000]}]
Example 2) new data g:{50}
then
Var ans =[{"b":[1,2]},{"g":[100,2,500]},{c:[2000]}]
I am a beginner in node js, understand array, object concept, but not getting exact logic!
Thanks!
You can try following:
Logic
Filter array based on key
Check if object with mentioned key exists or not.
If yes, push value to this array.
If not, create a dummy object and push this object to original array.
Correction, when you do .push({key: value}), key will be considered as string.
Alternates
If you are using ES6, .push({ [key] : value })
Create a dummy object var o = {}. Set key and value to it o[key] = value and push this object.
Optimisations
Instead of setting value like obj[key] = value, since we will be operating on arrays, try obj[key] = [].concat(value). This will enable you to pass value as number or array of values.
Instead of checking the existence of value in .filter, try Array.isArray to check if value exists and is of type array.
Custom function
function checkAndPush(array, key, value) {
var filteredList = array.filter(function(o) {
return Array.isArray(o[key]);
});
filteredList.length > 0 ? filteredList[0][key].push(value) : array.push({
[key]: [].concat(value)
});
return array;
}
var ans =[{"b":[1,2]},{"g":[100,2]}]
console.log(checkAndPush(ans, "c", [2,3]))
console.log(checkAndPush(ans, "c", 4));
Prototype function
Array.prototype.checkAndPush = function(key, value) {
var filteredList = this.filter(function(o) {
return Array.isArray(o[key]);
});
var dummy = {}
dummy[key] = [].concat(value)
filteredList.length > 0 ? filteredList[0][key].push(value) : this.push(dummy);
// or ES6: this.push({ [key]: [].concat(value) })
return this;
}
var ans =[{"b":[1,2]},{"g":[100,2]}]
console.log(ans.checkAndPush("c", [2,3]))
console.log(ans.checkAndPush("c", 4));
If you are dealing with objects as your values
ans[key] = ans[key] || []
ans[key].push(value)
Note, this works because your values will be an array. If they could be primatives then you would use hasOwnProperty to check.
if (ans.hasOwnProperty(key)) {
// Add this to your key somehow
} else {
// initialize the key with your value
}
Node.js is nothing but a library built on javascript. You can do anything using javascript type of progmming. However push and pop method should be able to help you to deal with nodejs array.
ans[key].push(value)

Categories

Resources