Find Index from Mapped Objects - javascript

Desired output will be like, I want to find last and first index. I'm having trouble in handling this. I'm not be able to get the index from this objects.
{
'13-2-2022' =>[
{
"name":"abc"
"date":"13-2-2022"
},
{
"name":"xyz"
"date":"13-2-2022"
}
]
'15-2-2022' =>[
{
"name":"azx"
"date":"15-2-2022"
},
{
"name":"qwe"
"date":"15-2-2022"
}
]
'16-2-2022' =>[
{
"name":"azx"
"date":"16-2-2022"
},
{
"name":"qwe"
"date":"16-2-2022"
}
{
"name":"qpe"
"date":"16-2-2022"
}
]
}

You can get the object keys with Object.keys function.
const keys = Object.keys(myMap);
const first = keys[0];
const last = keys[keys.length-1];

Related

How to convert string array into custom nested object

I am trying to convert a string array into single nested object suggest a best way to do that,
For example, If I had a single element in an array
Input:
var keys = ["A"];
It should be like this,
Output:
{
and: {
tags: {
contains: "A"
}
}
}
And if I add a second element for example,
Input:
var keys = ["A","B"];
It should add as a nested object like this,
Output:
{
and: {
tags: {
contains: "A"
},
and: {
tags: {
contains: "B"
}
}
}
}
You can try to use Array.reduceRight function(Thanks Drew Reese's suggestion):
const nest = arr => arr.reduceRight((acc, cur) => ({ and: { tags: { contains: cur }, ...acc } }), {});
console.log(nest(['A']));
console.log(nest(['A', 'B']));
console.log(nest(['A', 'B', 'C']));

Logic App/JavaScript - Remove Matching Values

I have two data sources. Data Source 1 is basically a list of Projects and Users assigned to those Projects. Data Source 2 is a list of Users that I want to remove from the Projects in Data Source 1.
Data Source 1:
[
{
"db1Project":[
{
"projectCode":"1",
"assignment":{
"db1User":[
{
"assignee":"wantzc"
},
{
"assignee":"michelles"
}
]
}
},
{
"projectCode":"2",
"assignment":{
"db1User":[
{
"assignee":"stallinga"
},
{
"assignee":"domanl"
},
{
"assignee":"brantleyd"
}
]
}
},
{
"projectCode":"3",
"assignment":{
"db1User":[
{
"assignee":"cinnamonk"
}
]
}
}
]
}
]
I want to match "assignee" from Data Source 1 with "sponsor" in Data Source 2.
Data Source 2:
[
{
"db2Users":[
{
"sponsor":"wantzc"
},
{
"sponsor":"patem"
},
{
"sponsor":"stallinga"
},
{
"sponsor":"oliviaa"
},
{
"sponsor":"brantleyd"
}
]
}
]
Then remove non-matching "assignee" and generate below output:
Desired Output:
[
{
"db1Project":[
{
"projectCode":"1",
"assignment":{
"db1User":[
{
"assignee":"wantzc"
}
]
}
},
{
"projectCode":"2",
"assignment":{
"db1User":[
{
"assignee":"stallinga"
},
{
"assignee":"brantleyd"
}
]
}
}
]
}
]
Can this be done using Logic App only? If not, how to do this using JavaScript?
This is a little bit of a verbose way to do it in Javascript, but the idea is relatively simple:
Create a map of the users you're looking for so you have an O(1) lookup instead of O(n) lookup each time
Filter the users list for each project to only contain users found in the db2Users list
Add the project to the new results set if any db1Users remain after step 2
This will take 1 pass through your original array, so it's an efficient, yet simple algorithm to do what you're trying to do.
let data = [{"projectCode":"1","assignment":{"db1User":[{"assignee":"wantzc"},{"assignee":"michelles"}]}},{"projectCode":"2","assignment":{"db1User":[{"assignee":"stallinga"},{"assignee":"domanl"},{"assignee":"brantleyd"}]}},{"projectCode":"3","assignment":{"db1User":[{"assignee":"cinnamonk"}]}}];
let db2Users = [{"sponsor":"wantzc"}, {"sponsor":"patem"}, {"sponsor":"stallinga"}, {"sponsor":"oliviaa"}, {"sponsor":"brantleyd"}];
let db2UsersMap = db2Users.reduce((res, curr) => {
res[curr.sponsor] = true;
return res;
}, {});
let filteredProjects = data.reduce((res, project) => {
let currProjUsers = project.assignment.db1User;
project.assignment.db1User = currProjUsers.filter((user) => db2UsersMap[user.assignee]);
if (project.assignment.db1User.length > 0) { res.push(project); }
return res;
}, []);
console.log(filteredProjects);

Retrieve each value from a specific key with json

I would like to know if there is a way to get every values of "-temp" from the .json
{
"weather":{
"notes":{
"cities":[
{
"-id":"scranton",
"-temp":"17"
},
{
"-id":"paris",
"-temp":"16"
},
{
"-id":"new york",
"-temp":"18"
}
]
}
}
}
How I tried to get it with JavaScript but that didn't work and I get undefined
data.weather.notes.cities['-temp']
How can I get every value of "-temp"?
You can use map:
const temps = data.weather.notes.cities.map(city => city["-temp"]);
console.log(temps); // ["17", "16", "18"]
Of course you can always access to them individually:
const { cities } = data.weather.notes;
console.log(cities[0]["-temp"]); // "17"
Or loop all of them:
for (let city of cities) {
console.log("temperature in %s is %s°",
city["-id"], city["-temp"]
);
}
You could possibly iterate through all the cities and gather the "-temp" keys.
data.weather.notes.cities.forEach(function(element) {
for (var em in element) {
if (em == "-temp")
{
console.log(element[em])
}
}
});
#ZER0 answer is probably the best though.
var data = {
"weather":{
"notes":{
"cities":[
{
"-id":"scranton",
"-temp":"17"
},
{
"-id":"paris",
"-temp":"16"
},
{
"-id":"new york",
"-temp":"18"
}
]
}
}
};
for(var i in data.weather.notes.cities) {
let city = data.weather.notes.cities[i];
console.log(city["-temp"]); //You_ can get it here
}
You can't use JSON the same way you use jquery selectors. In your case, you need to map your array of cities.
const object = {
"weather":{
"notes":{
"cities":[
{
"-id":"scranton",
"-temp":"17"
},
{
"-id":"paris",
"-temp":"16"
},
{
"-id":"new york",
"-temp":"18"
}
]
}
}
};
const tempList = object.weather.notes.cities.map(city => city['-temp']);
//result: ["17", "16", "18"]
See map documentation for further information.

I am trying to check if given value exist as key in array of objects

I am trying to check if given value exist as key in array of objects
var obj = [{
tapCount1: '10'
}, {
tapCount2: '500'
}, {
tapCount3: '1250'
}, {
tapCount4: '1250'
}, {
wtOfSample: '41.00'
}, {
initialVolume: '66.0'
}, {
tapCountvol1: '60.0'
}, {
tapCountvol2: '53.0'
}, {
tapCountvol3: '52.0'
}, {
tapDensity: '0.788'
}, {
compressibilityIndex: '21.212'
}, {
hausnerRatio: '1.269'
}];
i had use below code
if (arrTDTData.hasOwnProperty("tapCount1") == false) {
count1 = 0;
} else {
count1 = arrTDTData.tapCount1;
}
i want to check if key is equal tapCount1 then it will return true else flase```
If you want to check if there is an object in the array that has tapCount1 key, you can use some().
The some() method tests whether at least one element in the array
passes the test implemented by the provided function. It returns a
Boolean value.
var obj = [{"tapCount1":"10"},{"tapCount2":"500"},{"tapCount3":"1250"},{"tapCount4":"1250"},{"wtOfSample":"41.00"},{"initialVolume":"66.0"},{"tapCountvol1":"60.0"},{"tapCountvol2":"53.0"},{"tapCountvol3":"52.0"},{"tapDensity":"0.788"},{"compressibilityIndex":"21.212"},{"hausnerRatio":"1.269"}];
var result = obj.some(o => "tapCount1" in o);
console.log(result);
Use includes with map and Object.keys (and reduce to flatten the array):
var obj = [{tapCount1:'10'},{tapCount2:'500'},{tapCount3:'1250'},{tapCount4:'1250'},{wtOfSample:'41.00'},{initialVolume:'66.0'},{tapCountvol1:'60.0'},{tapCountvol2:'53.0'},{tapCountvol3:'52.0'},{tapDensity:'0.788'},{compressibilityIndex:'21.212'},{hausnerRatio:'1.269'}];
const res = obj.map(Object.keys).reduce((acc, curr) => acc.concat(curr)).includes("tapCount1");
console.log(res);
You can also use some on the array itself with hasOwnProperty (to avoid scanning the prototype):
var obj = [{tapCount1:'10'},{tapCount2:'500'},{tapCount3:'1250'},{tapCount4:'1250'},{wtOfSample:'41.00'},{initialVolume:'66.0'},{tapCountvol1:'60.0'},{tapCountvol2:'53.0'},{tapCountvol3:'52.0'},{tapDensity:'0.788'},{compressibilityIndex:'21.212'},{hausnerRatio:'1.269'}];
const res = obj.some(e => e.hasOwnProperty("tapCount1"));
console.log(res);
You could get a single object and check the wanted property.
var array = [{ tapCount1: '10' }, { tapCount2: '500' }, { tapCount3: '1250' }, { tapCount4: '1250' }, { wtOfSample: '41.00' }, { initialVolume: '66.0' }, { tapCountvol1: '60.0' }, { tapCountvol2: '53.0' }, { tapCountvol3: '52.0' }, { tapDensity: '0.788' }, { compressibilityIndex: '21.212' }, { hausnerRatio: '1.269' }],
tapCount1 = 'tapCount1' in Object.assign({}, ...array);
console.log(tapCount1);

Looping through an array of objects and find a specific key

I have an array of objects which looks like
data =
[
{
"AccountType":"Client",
"DeploymentList":
{
"-L3y8Kpl5rcvk-81q004":
{
"DeploymentKey":"-L3y8Kpl5rcvk-81q004",
"DeploymentName":"Testing 3"
}
}
},
{
"AccountType":"Client",
"DeploymentList":
{
"-L3yGFxXQ8XbeK8b2GSF":
{
"DeploymentKey":"-L3yGFxXQ8XbeK8b2GSF",
"DeploymentName":"Testing 1"
}
}
}
]
I want to loop through this data and want to find a string. In this data, I want to find
What I have tried so far is
for (let d of this.data) {
for(let a of d.DeploymentList){
if(a.$key==="-L3y8Kpl5rcvk-81q004"){
// Inside the condition
}
}
But it is not working. How I can achieve this ?
You can check if key exists as follows,
for (let d of this.data) {
for(let a of d.DeploymentList){
if(a["-L3y8Kpl5rcvk-81q004"]){
// Inside the condition
}
}
It depends on what you want to do. If you're just trying to find the deployment list item you can do it easily with find
var item = data.find(item => item.DeploymentList["-L3y8Kpl5rcvk-81q004"]);
This will give you the item from your data array with that particular DeploymentList item.
var data =
[
{
"AccountType":"Client",
"DeploymentList":
{
"-L3y8Kpl5rcvk-81q004":
{
"DeploymentKey":"-L3y8Kpl5rcvk-81q004",
"DeploymentName":"Testing 3"
}
}
},
{
"AccountType":"Client",
"DeploymentList":
{
"-L3yGFxXQ8XbeK8b2GSF":
{
"DeploymentKey":"-L3yGFxXQ8XbeK8b2GSF",
"DeploymentName":"Testing 1"
}
}
}
];
var item = data.find(item => item.DeploymentList["-L3y8Kpl5rcvk-81q004"]);
console.log(item);

Categories

Resources