I want to check if a value doesn't exist in the given object, by filtering an array of string.
I want to check if the values in the keys array are contained in the JSON object I'm looping. If one of the values isn't, I have to do something else, but only if the non-existent value (in resArray) is contained in the keys array.
JSON here
Here's what I tried:
var keys = [
"total_kills",
"total_deaths",
"total_planted_bombs",
"total_defused_bombs",
"total_kills_knife",
"total_kills_headshot",
"total_wins_pistolround",
"total_wins_map_de_dust2",
"last_match_wins",
"total_shots_fired",
"total_shots_hit",
"total_rounds_played",
"total_kills_taser",
"last_match_kills",
"last_match_deaths",
"total_kills_hegrenade",
];
var resArray = stats.playerstats.stats;
var statsArray = [];
for (var i = 0; i < keys.length; i++) {
for(var j = 0; j < resArray.length; j++){
//if the value in keys array exists, do something
if(resArray[j]["name"] === keys[i]){
//do something
}
if(<value doesn't exist)>)
//do something else.
}
}
Solved:
function contains(obj, key, value) {
return obj.hasOwnProperty(key) && obj[key] === value;
}
var resArray = stats.playerstats.stats;
var statsArray = [];
for (var i = 0; i < keys.length; i++) {
resArray.some(function(found){
if(contains(found, "name", keys[i])){
statsArray.push(found);
}
});
if(typeof statsArray[i] == 'undefined'){
console.log("Not present in array: " + keys[i]);
statsArray.push({"name": keys[i], "value": 'None'});
}
}
Thanks to everyone has replied to this thread.
Your example insinuates that you're creating a new array based off the stats and conditional presence of your provided keys. An easy way to build this array would be to use Array.prototype.map to enumerate over your stats array. Next, in each iteration's callback you can pass the name property as an argument to keys.indexOf to check if that particular name is present in your keys array.
var statsArray = stats.map(function(stat) {
if (keys.indexOf(stat.name) > -1) {
return stat;
} else {
return stat.name + ' not found.';
}
});
This will yield a new array which will contain either the stat object or a not regarding its absence in keys. However, you can return whatever your heart desires, as long as it's a valid array item.
Here's a working example with a small chunk of your dataset (but will work with your original dataset):
var keys = [
"total_kills",
"total_deaths",
"total_planted_bombs",
"total_defused_bombs",
"total_kills_knife",
"total_kills_headshot",
"total_wins_pistolround",
"total_wins_map_de_dust2",
"last_match_wins",
"total_shots_fired",
"total_shots_hit",
"total_rounds_played",
"total_kills_taser",
"last_match_kills",
"last_match_deaths",
"total_kills_hegrenade",
];
var stats = [{
"name": "total_kills",
"value": 25305
}, {
"name": "total_deaths",
"value": 27474
}, {
"name": "total_time_played",
"value": 1822419
}, {
"name": "total_planted_bombs",
"value": 1397
}, {
"name": "total_defused_bombs",
"value": 239
}, {
"name": "total_wins",
"value": 11477
}, {
"name": "total_damage_done",
"value": 3783962
}, {
"name": "total_money_earned",
"value": 65159500
}, {
"name": "total_rescued_hostages",
"value": 1
}, {
"name": "total_kills_knife",
"value": 278
}, {
"name": "total_kills_hegrenade",
"value": 168
}, {
"name": "total_kills_glock",
"value": 699
}, {
"name": "total_kills_deagle",
"value": 1289
}, {
"name": "total_kills_elite",
"value": 37
}, {
"name": "total_kills_fiveseven",
"value": 165
}, {
"name": "total_kills_xm1014",
"value": 78
}, {
"name": "total_kills_mac10",
"value": 154
}, {
"name": "total_kills_ump45",
"value": 330
}, {
"name": "total_kills_p90",
"value": 1105
}, {
"name": "total_kills_awp",
"value": 6934
}, {
"name": "total_kills_ak47",
"value": 4528
}, {
"name": "total_kills_aug",
"value": 137
}, {
"name": "total_kills_famas",
"value": 540
}, {
"name": "total_kills_g3sg1",
"value": 116
}, {
"name": "total_kills_m249",
"value": 50
}, {
"name": "total_kills_headshot",
"value": 7112
}, {
"name": "total_kills_enemy_weapon",
"value": 2308
}, {
"name": "total_wins_pistolround",
"value": 843
}, {
"name": "total_wins_map_cs_assault",
"value": 9
}, {
"name": "total_wins_map_cs_italy",
"value": 15
}, {
"name": "total_wins_map_cs_office",
"value": 11
}, {
"name": "total_wins_map_de_aztec",
"value": 71
}, {
"name": "total_wins_map_de_cbble",
"value": 373
}, {
"name": "total_wins_map_de_dust2",
"value": 4857
}, {
"name": "total_wins_map_de_dust",
"value": 25
}, {
"name": "total_wins_map_de_inferno",
"value": 777
}, {
"name": "total_wins_map_de_nuke",
"value": 247
}, {
"name": "total_wins_map_de_train",
"value": 47
}, {
"name": "total_weapons_donated",
"value": 2466
}, {
"name": "total_broken_windows",
"value": 30
}, {
"name": "total_kills_enemy_blinded",
"value": 566
}, {
"name": "total_kills_knife_fight",
"value": 67
}, {
"name": "total_kills_against_zoomed_sniper",
"value": 2284
}, {
"name": "total_dominations",
"value": 270
}, {
"name": "total_domination_overkills",
"value": 225
}, {
"name": "total_revenges",
"value": 207
}, {
"name": "total_shots_hit",
"value": 83704
}, {
"name": "total_shots_fired",
"value": 399207
}, {
"name": "total_rounds_played",
"value": 23419
}, {
"name": "total_shots_deagle",
"value": 12137
}, {
"name": "total_shots_glock",
"value": 21299
}, {
"name": "total_shots_elite",
"value": 777
}, {
"name": "total_shots_fiveseven",
"value": 3385
}, {
"name": "total_shots_awp",
"value": 22667
}];
var statsArray = stats.map(function(stat) {
if(keys.indexOf(stat.name) > -1) {
return stat;
} else {
return stat.name + ' not present in keys';
}
});
console.log(statsArray);
You can achieve what you want by using a combination of array functions. For example:
let stats = data.playerstats.stats;
let matches = stats.filter(i => keys.indexOf(i.name) >= 0);
let matchKeys = matches.map(k => k.name);
let negatives = keys.filter(i => matchKeys.indexOf(i) < 0);
Then you can just loop through the matches/negatives to do what you want with them.
Fiddle here.
Related
I have an array of objects like so:
arrayOfObjects = [
{
"name": "OneFullValue",
"value": 709
},
{
"name": "DRTG",
"value": 0
},
{
"name": "OtherFullValue",
"value": 345
},
{
"name": "TYU",
"value": 0
},
{
"name": "TRHY",
"value": 0
},
{
"name": "UOI",
"value": 0
},
]
End goal:
arrayOfObjectsEndGoal = [
{
"name": "OneFullValue",
"value": 709
},
{
"name": "DRTG - TYU - TRHY - UOI",
"value": 0
},
{
"name": "OtherFullValue",
"value": 345
}
]
Essentially, I want to group together all of the objects with the same value where the "name" key has the value of all of the names of the other objects separated by a -.
NOTE: THE VALUE CAN BE ANY NUMBER OTHER THAN 0, FOR EXAMPLE, IF I HAVE AN ARRAY LIKE SO:
arrayOfObjects = [
{
"name": "OneFullValue",
"value": 709
},
{
"name": "DRTG",
"value": 123
},
{
"name": "OtherFullValue",
"value": 345
},
{
"name": "TYU",
"value": 123
},
{
"name": "TRHY",
"value": 123
},
{
"name": "UOI",
"value": 123
},
]
End goal (we're not multiplying 123 by how many objects there are with value 123, we're just equating it):
arrayOfObjectsEndGoal = [
{
"name": "OneFullValue",
"value": 709
},
{
"name": "DRTG - TYU - TRHY - UOI",
"value": 123
},
{
"name": "OtherFullValue",
"value": 345
}
]
I know how to combine all of the objects together
arrayOfObjects.reduce(
((prev, oth) => Object.assign(prev, oth)), {}
)
I also know that I can use a foreach to loop through the array and manipulate the values:
let objItem = {};
let arrayOfObjectsEndGoal = [];
arrayOfObjects.foreach((obj) => {
if (objItem[obj.value]) {
let first = objItem[obj.name] -1;
let newValue = {
...arrayOfObjectsEndGoal[first]
};
arrayOfObjectsEndGoal[first] = newValue;
});
However, I'm unsure of how to move forwards to achieve the result. How can I combine objects like above?
You could add the names.
const
data = [{ name: "OneFullValue", value: 709 }, { name: "DRTG", value: 0 }, { name: "OtherFullValue", value: 345 }, { name: "TYU", value: 0 }, { name: "TRHY", value: 0 }, { name: "UOI", value: 0 }],
result = Object.values(data.reduce((r, { name, value }) => {
r[value] ??= { name: '', value };
r[value].name += (r[value].name && ' - ') + name;
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have an object that looks like the following {key: id numbers}
var obj = {
"c4ecb": {id: [3]},
"a4269": {id: [34,36]},
"d76fa": {id: [54,55,60,61]},
"58cb5": {id: [67]}
}
How do I loop each above id in the following array, and return the label?
var response =
{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}
Finally, what I want to do is look up the key and return a string of id values.
For example, input c4ecb and output strawberry. Input a4269 and output lettuce, radish. Input d76fa and output "spaghetti, rigatoni, lasagna, fettuccine"
I think to join the multiple labels output into one string I could use something like
array.data.vegetables.map(vegetables => vegetables.value).join(', ')].toString();
So in the end I want to have something like
var fruits = [some code that outputs "strawberry"];
var vegetables = [some code that outputs "lettuce, radish"];
var pasta = [some code that outputs "spaghetti, rigatoni, lasagna, fettuccine"];
What I've tried so far:
The following loop will return the id only if there is one id to be called for: e.g. only in case one where {id: 3} but returns null in cases like {id: 34,36} (because it's looking for '34,36' in id, which doesn't exist - I need to look for each one individually.
response.data.forEach(({key, options}) => {
if (obj[key]) {
options.forEach(({id, label}) => {
if (id == obj[key].id) obj[key].label = label;
});
}
});
console.log(obj)
Filter the response object to focus on the category that matches the id.
Map over the options array and select the items which appear in obj[id].
Finally convert the filtered results to a string.
See filteredLabelsAsString() function below for implementation.
var obj = {
"c4ecb": {"id": [3]},
"a4269": {"id": [34,36]},
"d76fa": {"id": [54,55,60,61]},
"58cb5": {"id": [67]}
}
var response =
[{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}];
function filteredLabelsAsString(obj_key, obj, content=response) {
// sanity check: obj must contain obj_key
if (Object.keys(obj).includes(obj_key)) {
return content.filter((item) => {
// filter content using value of obj_key
return item.data[0].key == obj_key;
}).map((item) => {
// item : { success: true, data: [] }
// map over options array
return item.data[0].options.map((opt) => {
// option : {id, label}
// return the label if the id is in the obj object's list
if (obj[item.data[0].key].id.includes(opt.id))
return opt.label;
}).filter((label) => {
// filter out empty items
return label !== undefined;
});
}).join(",");
}
// if obj does not contain obj_key return empty string
return "";
}
console.log("fruits: " + filteredLabelsAsString("c4ecb", obj));
console.log("vegetables: " + filteredLabelsAsString("a4269", obj));
console.log("pasta: " + filteredLabelsAsString("d76fa", obj));
I am pretty new to learning to code. So sorry if this is a stupid question.
I have a nested object database that I want to search for a character name and then return to me who's character it is. But, so far, I can only find solutions that search the top level objects or are for arrays and I am running out of ideas.
Is it possible to search in depth for a name like 'Farah' and then somehow get 'olis characters' back?
Thanks in advance for any advice you guys might have!
{
"olis characters": {
"0": {
"name": "Farah",
"class": "rogue",
"level": 74
},
"1": {
"name": "Grop",
"class": "paladin",
"level": 31
},
"2": {
"name": "Skolmr",
"class": "druid",
"level": 85,
}
},
"chris characters": {
"0": {
"name": "Trygve",
"class": "bard",
"level": 28
},
"1": {
"name": "Brusi",
"class": "rogue",
"level": 10
},
"2": {
"name": "Steini",
"class": "skald",
"level": 58
}
}
}
As it is, your data is a little odd. You have and object with numeric keys, which suggests it should be an array. Having said that you can still search through the Object.values to get the data you want.
let data = {"olis characters": {"0": {"name": "Farah","class": "rogue","level": 74},"1": {"name": "Grop","class": "paladin","level": 31},"2": {"name": "Skolmr","class": "druid","level": 85,}},"chris characters": {"0": {"name": "Trygve","class": "bard","level": 28},"1": {"name": "Brusi","class": "rogue","level": 10},"2": {"name": "Steini","class": "skald","level": 58}}}
function findChar(name, data) {
for (let charGroup of Object.values(data)) {
let found = Object.values(charGroup).find(char => char.name === name)
if (found) return found
}
}
console.log(findChar('Grop', data))
console.log(findChar('Brusi', data))
// will return undefined if the name is not there:
console.log(findChar('Mark', data))
If you changed the data model to a simple array like:
let data = {
"olis characters": [{
"name": "Farah",
"class": "rogue",
"level": 74
},
{
"name": "Grop",
"class": "paladin",
"level": 31
}
],
"chris characters": [{
"name": "Trygve",
"class": "bard",
"level": 28
},
// ...
]
}
...you could avoid one of those Object.values and use find directly.
function findChar(name, data){
for (let charGroup of Object.values(data)){
let found = charGroup.find(char => char.name === name)
if (found) return found
}
}
It gets harder to run a loop through an object like this, maybe an object of objects is not the right data structure, consider using an array of object instead.
But May be this code can help you
const data = {
"olis characters": {
"0": {
"name": "Farah",
"class": "rogue",
"level": 74
},
"1": {
"name": "Grop",
"class": "paladin",
"level": 31
},
"2": {
"name": "Skolmr",
"class": "druid",
"level": 85,
}
},
"chris characters": {
"0": {
"name": "Trygve",
"class": "bard",
"level": 28
},
"1": {
"name": "Brusi",
"class": "rogue",
"level": 10
},
"2": {
"name": "Steini",
"class": "skald",
"level": 58
}
}
}
Object.keys(data).forEach(item => {
if(findItemByName('Farah', data[item])){
console.log(item)
}
})
function findItemByName(name, item) {
let r = false;
Object.keys(item).forEach(obj => {
if (item[obj].name == name) {
r = true;
}
});
return r;
}
So in the API response example below, focusing on env_variables, I am trying grab the value for secret. I am stuck because as you can see, the name and value are not nested together. I am not familiar with how to grab the value based on the name in this example.
api response:
{
"id": 1146,
"job": {
"name": "jobname1",
},
"env_variables": [
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
],
},
{
"id": 1147,
"job": {
"name": "jobname2",
},
"env_variables": [
{
"name": {
"name": "secret"
},
"value": {
"value": "10.13.7"
}
},
{
"name": {
"name": "test5"
},
"value": {
"value": "10.13.6"
}
},
],
}
js
jobs: []
apiEndpoint = "test.com/api"
fetch(this.apiEndpoint)
.then(response => response.json())
.then(body => {
for(let i=0; i<body.length; i++){
this.jobs.push({
'build_id': JSON.stringify(body[i].id),
'secret': //not sure how to pull the value (10.13.7)
})
}
})
You need nested loops, since there are two nested arrays: the top level of the response is an array of objects, and env_variables contains an array of objects.
fetch(this.apiEndpoint)
.then(response => response.json())
.then(body => {
for (let i = 0; i < body.length; i++) {
let env = body[i].env_variables;
for (let j = 0; j < env.length; j++) {
if (env[j].name.name == "secret") {
this.jobs.push({
'build_id': JSON.stringify(body[i].id),
'secret': env[j].value.value
})
}
}
}
})
You can do something like this inside .then(body=>...
const body = [{ //it looks like brackets [] were lost in OP
"id": 1146,
"job": {
"name": "jobname1",
},
"env_variables": [{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
],
},
{
"id": 1147,
"job": {
"name": "jobname2",
},
"env_variables": [{
"name": {
"name": "secret"
},
"value": {
"value": "10.13.7"
}
},
{
"name": {
"name": "test5"
},
"value": {
"value": "10.13.6"
}
},
],
}
];
let secret = null;
body.forEach(b => {
let el = b.env_variables.find(e => e.name.name == 'secret');
if (el) { //found
secret = el.value.value;
return false; //exit forEach
}
});
console.log(secret);
You could also do something like this with Array.forEach and Array.find:
let data = [{ "id": 1146, "job": { "name": "jobname1", }, "env_variables": [{ "name": { "name": "test1" }, "value": { "value": "10.13.6" } }, { "name": { "name": "test1" }, "value": { "value": "10.13.6" } }, ], }, { "id": 1147, "job": { "name": "jobname2", }, "env_variables": [{ "name": { "name": "secret" }, "value": { "value": "10.13.7" } }, { "name": { "name": "test5" }, "value": { "value": "10.13.6" } }, ], } ]
let jobs = []
data.forEach(({id, env_variables}) => jobs.push({
build_id: id,
secret: ((env_variables.find(({name}) =>
name.name === 'secret') || {}).value || {}).value || 'N/A'
// ... other props
}))
console.log(jobs)
Assuming your result is an array, you could do something like this:
let secrets = results.reduce((result, item) => {
let secret = item["env_variables"].find((v) => {return v.name.name === "secret"})
if(secret){
result.push({id:item.id, secret: secret.value.value});
}
return result;
}, []);
This would return an array of objects like {id: 1, secret: ""} for each object in your result set that has a secret.
If you don't care whether the secret is present or not, you could modify the code slightly like this:
let secrets = results.reduce((result, item) => {
let secret = item["env_variables"].find((v) => {return v.name.name === "secret"})
result.push({id:item.id, secret: secret ? secret.value.value : ""});
return result;
}, []);
Which just leaves with you an empty string on the levels where there is no secret.
I'm facing some issue in for loop while creating an object from array of object.I have an array as this in node js app:
[
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
]
I want to return object like this which contains all the Material as array, Name and there value in array of object like this:
{
Material: ["113/133", "150/300"],
datasets: [
{
label: "WELD1",
data: [27520,1441]
},
{
label: "WELD2",
data: [676992,555]
},
{
label: "WELD3",
data: [100,20,0]
}
]
}
I want to get result using for loop.
you can use .reduce() and do something like this:
var arr = [
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
];
var newArr = arr.reduce((acc, ob) => {
for (var key in ob)
if(typeof acc[key] === 'object')
acc[key] = acc[key] ? acc[key].concat(ob[key]) : [ob[key]];
else
acc[key] ? acc[key].push(ob[key]) : acc[key] = [ob[key]];
return acc;
}, {});
console.log(newArr);
let array = [
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
]
let answer = {Material: [], datasets: []}
array.forEach(x => {
answer.Material.push(x.Material);
x.Name.forEach(na => {
let object = answer.datasets.find(obj => obj.label === na.name) || {label: "", data: []};
if(object.label === ""){
object.label = na.name;
object.data.push(na.value);
answer.datasets.push(object);
}else{
object.data.push(na.value)
}
});
});
console.log(answer);
The above is alternative solution using forEach instead of reduce
Use of Array.reduce to build your new data structure using data you have
const start = [{
"Material": "113/133",
"Name": [{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
];
const end = start.reduce((tmp, {
Material,
Name,
}) => {
// Handle the material
// If it do not exist in the array, push it
if (!tmp.Material.includes(Material)) {
tmp.Material.push(Material);
}
// Handle the datasets
// Look at each Name
Name.forEach(({
name,
value,
}) => {
// Can we find the label?
const labelFind = tmp.datasets.find(y => y.label === name);
// If we can't find the label, create a new dataset
if (!labelFind) {
tmp.datasets.push({
label: name,
data: [
value,
],
});
return;
}
// If we has found it push new value in the dataset
labelFind.data.push(value);
});
return tmp;
}, {
Material: [],
datasets: [],
});
console.log(end);
// This is the old fashioned way.
// Iterate over whole array,
// make a map, push value where 'name' is found in map
// later iterate over this map - dataMap - and form required datasets array.
var Material = [];
var dataMap = {};
arr.forEach(obj => {
Material.push(obj.Material);
obj.Name.forEach(item => {
if(dataMap[item.name]){
dataMap[item.name].push(item.value);
}
else {
dataMap[item.name] = [item.value];
}
});
});
var datasets = [];
Object.keys(dataMap).forEach(label => {
datasets.push({
label: label,
data: dataMap[label]
});
});
var result = {
Material: Material,
datasets: datasets
}
console.log(result);