I need to find out if one user appears more than once in the associative array (and then sum the value of the same tasks). How do I do that in javaScript?
{
"Items": [
{
"Date": {
"N": "1439883817221"
},
"UserName": {
"S": "user1"
},
"task1": {
"N": "9"
}
},
{
"Date": {
"N": "1439892361108"
},
"task2": {
"N": "3"
},
"UserName": {
"S": "user2"
}
},
{
"Date": {
"N": "1439904242126"
},
"UserName": {
"S": "user1"
},
"task2": {
"N": "2"
}
}
}}
Try this:
var uniqueNames = [];
var nonUniqueNames = [];
$.each( list.Items, function(i, item ) {
if(uniqueNames.indexOf(item.UserName.S) != -1 ){
nonUniqueNames.push(item.UserName.S);
} else {
uniqueNames.push(item.UserName.S);
}
});
alert(nonUniqueNames);
https://jsfiddle.net/c2co1hck/
As for the same tasks you can figure out the same way to do it.
Please define the same tasks anyway as you have different names and values for it.
Posting some of your code would also be appreciated.
By the way you have an error in your JSON, it should end with
]} not with }}
Related
I am trying to make a flashcard application to try and simplify data.
I think that this isn't a very difficult doubt but I am unable to find an answer from google.
I want only the key url and the value to be shown from the nested dictionary
I want to display the route in the form of a arrow map like: abcd->cd->d->url:value
my data ex:
abcd = {
"ab": {
"a": {
"country": "",
"url": ""
},
"b": {
"country": "",
"url": ""
}
},
"cd": {
"c": {
"country": "",
"url": ""
},
"d": {
"country": "",
"url": "value"
}
}
You can use the code given below which works for this object only ...
Note that there are a lot of better ways you can approach this .. this is a simple enough one
let abcd = {
"ab": {
"a": {
"country": "",
"url": "fff"
},
"b": {
"country": "",
"url": "fff"
}
},
"cd": {
"c": {
"country": "",
"url": "ddd"
},
"d": {
"country": "",
"url": "value"
}
}
}
function getKeyValue(obj){
const result = []
for(let prop in obj){
if(typeof obj[prop] === "object"){
for(let pr in obj[prop]){
result.push(`${prop}->${pr}->url->${obj[prop][pr].url }`)
}
}
}
return result
}
console.log(getKeyValue(abcd))
I have an object received in response from backend, and would like to extract elements of object and attach them to scope to make it available in the View.
Following is the structure of the object:
{
"Name": {
"S": "Jon Snow"
},
"Location": {
"S": "Winterfell"
},
"Details": {
"M": {
"Parents": {
"M": {
"mother": {
"S": "Lynna Stark"
}
}
},
"Dog": {
"N": "Ghost Snow"
}
}
}
}
Since I have received it from backend, I don't know what kind of object is this, and I want to convert it to a plain JSON object which should be looking something like this:
{
"Name": "Jon Snow",
"Location": "Winterfell",
"Details": {
"Parents": {
"mother": "Lynna Stark"
},
"Dog": "Ghost Snow"
}
}
Help is appreciated, as I am a beginner in AngularJS and also It would be good if someone elaborates what kind of Object did I receive? Thanks in advance.
Update 1:
Thanks for the responses. Now I have got the idea. Now the question is how to I flatten the object by one level? And If I do flatten does it tamper the original response as it is received from the backend, it may be different every time.
const data = {
"Name": {
"S": "Jon Snow"
},
"Location": {
"S": "Winterfell"
},
"Details": {
"M": {
"Parents": {
"M": {
"mother": {
"S": "Lynna Stark"
}
}
},
"Dog": {
"N": "Ghost Snow"
}
}
}
}
const flatten = (data) =>{
if(typeof data === "string") return data;
for(let key in data){
for(let deep in data[key]){
if(deep.length === 1){
const temp = data[key]
data[key] = flatten(temp[deep])
}
}
}
return data;
}
console.log( JSON.stringify(flatten(data), null, "\t"))
JS bin
I get data from an api, and this data contains one key, that last contains multiple keys, This is my example :
{
"status": 0,
"location": "Casablanca [Doukkala-Abda;Morocco]",
"day": {
"1": {
"date": "20200807",
"name": "Friday",
"month": "", ...
The parent key is the day
The child keys are 1, 2, 3 ...etc, they contains data
I tried to use console.log(data['day']['1']) and console.log(data.day['1']) but I got this error :
Cannot read property '1' of undefined
How do I read this properly ?
Just use the array form when the index is an interger, such as:
let data = {
"day": {
"1": {
"date" : "20200807"
}
}
}
console.log("data:", data);
console.log('data.day["1"]:', data.day["1"]);
console.log('data.day["1"].date:', data.day["1"].date);
You might want to remove the quotes like: data.day[1] instead of data.day['1']
var data =
{
"status": 0,
"location": "Casablanca [Doukkala-Abda;Morocco]",
"day": {
"1": {
"date": "20200807",
"name": "Friday",
"month": ""
},
"2": {
"date": "20200801",
"name": "test",
"month": ""
}
}
}
console.log(data.day[1]);
I assume the issue is that you need to parse your API response with JSON.parse() first, like:
let rawDataFromApi = '{"status":0,"location":"Casablanca [Doukkala-Abda;Morocco]","day":{"1":{"date":"20200807","name":"Friday","month":""}}}'
let data = JSON.parse(rawDataFromApi)
console.log(data['day']['1'])
. operator can be used to get the data.the data is a object so that [] will not be a good idea.
use day.1.date to get the data
Try this.
let data = {
"day": {
"1": {
"date" : "202008071"
}
}
}
let data2 = {
"day": {
"2": {
"date" : "202008072"
}
}
}
let data3 = {
"day": {
"third": {
"date" : "202008073"
}
}
}
console.log(data.day["1"].date);
console.log(data2.day["2"].date);
console.log(data3.day["third"].date);
With some JavaScript, how can I transform a JSON from:
{
"d": {
"__count": "13",
"results": [
{
"__metadata": {
"id": "123"
},
"COAST": "East",
"STATUS": "done",
"COLOR": "blue",
}
]
}
}
TO
{
"__count": "13",
"data": [
{
"__metadata": {
"id": "123"
},
"COAST": "East",
"STATUS": "done",
"COLOR": "blue",
}
]
}
Basically removing the extra "d" parent and renaming results to data? I am using this in the context of vue-table in VueJS.
Assumed that you have the json saved in a variable 'data':
data = data.d
data.data = data.results
delete data.results
This function will do it.
function transform(json) {
var obj = JSON.parse(json);
obj.d.data = obj.d.result;
delete obj.d.result;
return JSON.stringify(obj.d);
}
One solution is to unserialize your JSON to have an object (JSON.parse()). Then to serialize only what you need (JSON.stringify()).
You can use a loop.
var res = [];
for(var k in jsonData){
res.push(jsonData[k]);
}
var jsonData = {
"d": {
"__count": "13",
"results": [
{
"__metadata": {
"id": "123"
},
"COAST": "East",
"STATUS": "done",
"COLOR": "blue",
}
]
}
};
console.log(jsonData);
var res = [];
for(var k in jsonData){
res.push(jsonData[k]);
}
console.log("result:");
console.log(res);
I have following array:
var array = [
{
"milestoneTemplate": {
"id": "1",
"name": "TEST1"
},
"id": "1",
"date": "1416680824",
"type": "ETA",
"note": "Note",
"color": "66FF33"
},
{
"milestoneTemplate": {
"id": "2",
"name": "Test 2"
},
"id": "2",
"date": "1416680824",
"type": "ATA",
"note": "Note 22",
"color": "66FF00"
}
];
And now i would like to check in forEach loop that object (which is passed in param of the function) is existing in array by his ID.
In case that not = do push into existing array.
arrayOfResults.forEach(function(entry) {
if(entry != existingInArrayByHisId) {
array.push(entry);
}
});
Thanks for any advice
You could create a helper function thats checks if an array contains an item with a matching property value, something like this:
function checkForMatch(array, propertyToMatch, valueToMatch){
for(var i = 0; i < array.length; i++){
if(array[i][propertyToMatch] == valueToMatch)
return true;
}
return false;
}
which you can then use like so:
arrayOfResults.forEach(function (entry) {
if (!checkForMatch(array, "id", entry.id)) {
array.push(entry);
}
});