Removing the commas between objects in a JSON array - javascript

I am working on loading JSON data into Redshift, but for it to work the commas have to be removed between the objects. If I remove the commas then it works fine.
Can someone tell me how to remove the commas between objects so that I can load it into Redshift?
The Redshift copy command allows only arrays of objects to be loaded as rows.
Currently I have this JSON:
[{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri,
23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,"height":48
}
}
},
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}]
I need to transform it to this:
{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri,
23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,"height":48
}
}
}
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}

You could:
json parse your code,
loop on rows
output each row as json stringified
As:
// Your Array as String
let myArray_as_string = `
[
{
"a": "first", "objet": "with commas"
},
{
"an": "other", "objet": "2"
},
{
"a": "third", "objet": "3"
}
]
`;
// JSON parse the string to get a JS Object
let myArray_as_object = JSON.parse(myArray_as_string);
// Make a string with each stringified row
let myArray_without_commas = myArray_as_object.map( row => {
return JSON.stringify(row);
}).join("\n")
// Do something with the result value
console.log(myArray_without_commas);

let data = [{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri, 23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}]
output = data.map(function(e){return JSON.stringify(e,null,2)}).join("\n")
console.log(output);
If you already have a string representation. Works as long as there is no JSON strings inside the JSON object.
let data = JSON.stringify([{
"id":"57e4d12e53a5a",
"body":"asdas",
"published":"Fri, 23 Sep 2016 06:52:30 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"57e4d51165d97",
"body":"jackiechanSADAS",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
},
{
"id":"asas",
"body":"peterting",
"published":"Fri, 23 Sep 2016 07:09:05 +0000",
"type":"chat-message",
"actor":
{
"displayName":"beau",
"objectType":"person",
"image":
{
"url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
"width":48,
"height":48
}
}
}],null,2);
output = data.replace(/^\[/,"").replace(/]$/,"").replace(/}\,[\s]+{/g,"}\n\n{")
console.log(output);

Related

How to combine duplicates in associative array?

What is the most concise and efficient way to find out if a JavaScript array contains duplicates and merge them into new array please ?
I tried Lodash / d3 / underscoreJs but none of them generate clean result so I tried this code ->
var arr = [
{
"title": "My unique title",
"link": "domainlinkto-my-unique-title",
"image": "someurlto/my-unique-title-image",
"date": "Mon, 29 Jul 2019 02:25:08 -0000",
"site": "site1"
},
{
"title": "A duplicate title",
"link": "somedomainlinkto-a-duplicate-title/",
"image": "randomurlto/a-duplicate-title.jpg",
"date": "Sun, 25 Aug 2019 15:52:59 -0000",
"site": "site1"
},
{
"title": "A duplicate title",
"link": "otherdomainlinkto-a-duplicate-title/",
"image": "anotherurlto/duplicate-title.jpg",
"date": "Sun, 25 Aug 2019 21:09:37 -0000",
"site": "site2"
},
{
"title": "A DUPLICATE TITLE",
"link": "someotherdomainlinkto-a-duplicate-title/",
"image": "someurlto/aduplicatetitle.jpg",
"date": "Sat, 24 Aug 2019 18:43:38 -0000",
"site": "site3"
},
{
"title": "Other duplicate: title",
"link": "anydomainlinkto-other-duplicate-title/",
"image": "anotherdomainurlto/other-duplicate-title.jpg",
"date": "Mon, 26 Aug 2019 00:37:28 -0000",
"site": "site2"
},
{
"title": "Other duplicate : title",
"link": "anyotherdomainlinkto-other-duplicate-title/",
"image": "exampleurlto/hjKGHK45huu.jpg",
"date": "Mon, 26 Aug 2019 00:37:28 -0000",
"site": "site5"
},
{
"title": "Other unique title",
"link": "anydomainlinkto-other-unique-title/",
"image": "anyotherurlto/img/other-title.jpg",
"date": "Mon, 26 Aug 2019 09:18:10 -0000",
"site": "site3"
}
];
Array.prototype.groupBy = function (props) {
var arr = this;
var partialResult = {};
var imgResult = {};
arr.forEach(el=>{
var grpObj = {};
var grpImg = {};
props.forEach(prop=>{
grpObj.title = el.title;
grpImg.image = el.image;
});
var key = JSON.stringify(grpObj);
var keyImg = JSON.stringify(grpImg);
if(!imgResult[key]) {
imgResult[key] = grpImg.image;
} else {
imgResult[key] = el.image;
}
if(!partialResult[key]) partialResult[key] = [];
partialResult[key].push(
{
link: el.link,
site: el.site,
date: el.date
});
});
var finalResult = Object.keys(partialResult, imgResult).map(key=>{
var keyObj = JSON.parse(key);
keyObj.links = partialResult[key];
keyObj.image = imgResult[key];
return keyObj;
})
return finalResult;}
var filtered = arr.groupBy(['title']);
console.log(filtered);
BUT...
As you can see [titles] in UPPERCASE and "Other duplicate : title" is not taken as duplicate
WHAT I WOULD DO --->
var expected = [
{
"title": "My unique title",
"links": [{"date": "Mon, 29 Jul 2019 02:25:08 -0000","site": "site1", "link": "domainlinkto-my-unique-title"}],
"image": "someurlto/my-unique-title-image",
},
{
"title": "My duplicate title",
"links": [
{"date": "Sun, 25 Aug 2019 15:52:59 -0000","site": "site1","link":"somedomainlinkto-a-duplicate-title/"},
{"date": "Sun, 25 Aug 2019 21:09:37 -0000","site": "site2","link": "otherdomainlinkto-a-duplicate-title/"},
{"date": "Sat, 24 Aug 2019 18:43:38 -0000","site": "site3","link": "someotherdomainlinkto-a-duplicate-title/"}
],
"image": "randomurlto/a-duplicate-title.jpg",
},
{
"title": "Other duplicate: title",
"links": [
{"date": "Sun, 25 Aug 2019 15:52:59 -0000","site": "site2","link":"anydomainlinkto-other-duplicate-title/"},
{"date": "Mon, 26 Aug 2019 00:37:28 -0000","site": "site5","link": "anyotherdomainlinkto-other-duplicate-title/"}
],
"image": "anotherdomainurlto/other-duplicate-title.jpg",
},
{
"title": "Other unique title",
"links": [{"date": "Mon, 26 Aug 2019 09:18:10 -0000","site": "site1", "link": "anydomainlinkto-other-unique-title/"}],
"image": "anyotherurlto/img/other-title.jpg",
"site": "site3"
}
];
console.log(expected);
Hi Genious
What is the most concise and efficient way to find out if a JavaScript array contains duplicates and merge them into new array please ?
I tried Lodash / d3 / underscoreJs but none of them generate clean result so I tried this code ->
var arr = [
{
"title": "My unique title",
"link": "domainlinkto-my-unique-title",
"image": "someurlto/my-unique-title-image",
"date": "Mon, 29 Jul 2019 02:25:08 -0000",
"site": "site1"
},
{
"title": "A duplicate title",
"link": "somedomainlinkto-a-duplicate-title/",
"image": "randomurlto/a-duplicate-title.jpg",
"date": "Sun, 25 Aug 2019 15:52:59 -0000",
"site": "site1"
},
{
"title": "A duplicate title",
"link": "otherdomainlinkto-a-duplicate-title/",
"image": "anotherurlto/duplicate-title.jpg",
"date": "Sun, 25 Aug 2019 21:09:37 -0000",
"site": "site2"
},
{
"title": "A DUPLICATE TITLE",
"link": "someotherdomainlinkto-a-duplicate-title/",
"image": "someurlto/aduplicatetitle.jpg",
"date": "Sat, 24 Aug 2019 18:43:38 -0000",
"site": "site3"
},
{
"title": "Other duplicate: title",
"link": "anydomainlinkto-other-duplicate-title/",
"image": "anotherdomainurlto/other-duplicate-title.jpg",
"date": "Mon, 26 Aug 2019 00:37:28 -0000",
"site": "site2"
},
{
"title": "Other duplicate : title",
"link": "anyotherdomainlinkto-other-duplicate-title/",
"image": "exampleurlto/hjKGHK45huu.jpg",
"date": "Mon, 26 Aug 2019 00:37:28 -0000",
"site": "site5"
},
{
"title": "Other unique title",
"link": "anydomainlinkto-other-unique-title/",
"image": "anyotherurlto/img/other-title.jpg",
"date": "Mon, 26 Aug 2019 09:18:10 -0000",
"site": "site3"
}
];
Array.prototype.groupBy = function (props) {
var arr = this;
var partialResult = {};
var imgResult = {};
arr.forEach(el=>{
var grpObj = {};
var grpImg = {};
props.forEach(prop=>{
grpObj.title = el.title;
grpImg.image = el.image;
});
var key = JSON.stringify(grpObj);
var keyImg = JSON.stringify(grpImg);
if(!imgResult[key]) {
imgResult[key] = grpImg.image;
} else {
imgResult[key] = el.image;
}
if(!partialResult[key]) partialResult[key] = [];
partialResult[key].push(
{
link: el.link,
site: el.site,
date: el.date
});
});
var finalResult = Object.keys(partialResult, imgResult).map(key=>{
var keyObj = JSON.parse(key);
keyObj.links = partialResult[key];
keyObj.image = imgResult[key];
return keyObj;
})
return finalResult;}
var filtered = arr.groupBy(['title']);
console.log(filtered);
BUT...
As you can see [titles] in UPPERCASE and "Other duplicate : title" is not taken as duplicate
WHAT I WOULD DO --->
[
{
"title": "My unique title",
"links": [{"date": "Mon, 29 Jul 2019 02:25:08 -0000","site": "site1", "link": "domainlinkto-my-unique-title"}],
"image": "someurlto/my-unique-title-image",
},
{
"title": "My duplicate title",
"links": [
{"date": "Sun, 25 Aug 2019 15:52:59 -0000","site": "site1","link":"somedomainlinkto-a-duplicate-title/"},
{"date": "Sun, 25 Aug 2019 21:09:37 -0000","site": "site2","link": "otherdomainlinkto-a-duplicate-title/"},
{"date": "Sat, 24 Aug 2019 18:43:38 -0000","site": "site3","link": "someotherdomainlinkto-a-duplicate-title/"}
],
"image": "randomurlto/a-duplicate-title.jpg",
},
{
"title": "Other duplicate: title",
"links": [
{"date": "Sun, 25 Aug 2019 15:52:59 -0000","site": "site2","link":"anydomainlinkto-other-duplicate-title/"},
{"date": "Mon, 26 Aug 2019 00:37:28 -0000","site": "site5","link": "anyotherdomainlinkto-other-duplicate-title/"}
],
"image": "anotherdomainurlto/other-duplicate-title.jpg",
},
{
"title": "Other unique title",
"links": [{"date": "Mon, 26 Aug 2019 09:18:10 -0000","site": "site1", "link": "anydomainlinkto-other-unique-title/"}],
"image": "anyotherurlto/img/other-title.jpg",
"site": "site3"
}
];
I'm sure this is not the better way to do that (we are agree) so I'm asking stackoverflow genious...
Thanks for reading and time spent for thinking about my problem
I'd just lowercase the title before building up the json object for grouping. And I'd use object destructuring to clean up things, as well as just one hashtable, and I don't see the sense in a generic Array.prototype.groupBy if you hardcode properties in it:
const hash = {}, result = [];
for(const { title, link, image, date, site } of input) {
const key = JSON.stringify({ title: title.toLowerCase().replace(/ /g, ""), });
if(hash[key]) {
hash[key].push({ link, date, site });
} else {
result.push({ title, image, links: hash[key] = [{ link, date, site }], });
}
}
Seems like you want to group by lowercase titles without spaces :
var arr = [{"title":"My unique title","link":"domainlinkto-my-unique-title","image":"someurlto/my-unique-title-image","date":"Mon, 29 Jul 2019 02:25:08 -0000","site":"site1"},{"title":"A duplicate title","link":"somedomainlinkto-a-duplicate-title/","image":"randomurlto/a-duplicate-title.jpg","date":"Sun, 25 Aug 2019 15:52:59 -0000","site":"site1"},{"title":"A duplicate title","link":"otherdomainlinkto-a-duplicate-title/","image":"anotherurlto/duplicate-title.jpg","date":"Sun, 25 Aug 2019 21:09:37 -0000","site":"site2"},{"title":"A DUPLICATE TITLE","link":"someotherdomainlinkto-a-duplicate-title/","image":"someurlto/aduplicatetitle.jpg","date":"Sat, 24 Aug 2019 18:43:38 -0000","site":"site3"},{"title":"Other duplicate: title","link":"anydomainlinkto-other-duplicate-title/","image":"anotherdomainurlto/other-duplicate-title.jpg","date":"Mon, 26 Aug 2019 00:37:28 -0000","site":"site2"},{"title":"Other duplicate : title","link":"anyotherdomainlinkto-other-duplicate-title/","image":"exampleurlto/hjKGHK45huu.jpg","date":"Mon, 26 Aug 2019 00:37:28 -0000","site":"site5"},{"title":"Other unique title","link":"anydomainlinkto-other-unique-title/","image":"anyotherurlto/img/other-title.jpg","date":"Mon, 26 Aug 2019 09:18:10 -0000","site":"site3"}]
var result = arr.reduce((o, { title, image, link, date, site }, k) => (
( o[k = title.toLowerCase().replace(/ /g, '')] = o[k] || { title, image, links: [] } )
.links.push({ date, site, link }), o), {})
console.log( Object.values(result) )
If it has to work in IE too :
var arr = [{"title":"My unique title","link":"domainlinkto-my-unique-title","image":"someurlto/my-unique-title-image","date":"Mon, 29 Jul 2019 02:25:08 -0000","site":"site1"},{"title":"A duplicate title","link":"somedomainlinkto-a-duplicate-title/","image":"randomurlto/a-duplicate-title.jpg","date":"Sun, 25 Aug 2019 15:52:59 -0000","site":"site1"},{"title":"A duplicate title","link":"otherdomainlinkto-a-duplicate-title/","image":"anotherurlto/duplicate-title.jpg","date":"Sun, 25 Aug 2019 21:09:37 -0000","site":"site2"},{"title":"A DUPLICATE TITLE","link":"someotherdomainlinkto-a-duplicate-title/","image":"someurlto/aduplicatetitle.jpg","date":"Sat, 24 Aug 2019 18:43:38 -0000","site":"site3"},{"title":"Other duplicate: title","link":"anydomainlinkto-other-duplicate-title/","image":"anotherdomainurlto/other-duplicate-title.jpg","date":"Mon, 26 Aug 2019 00:37:28 -0000","site":"site2"},{"title":"Other duplicate : title","link":"anyotherdomainlinkto-other-duplicate-title/","image":"exampleurlto/hjKGHK45huu.jpg","date":"Mon, 26 Aug 2019 00:37:28 -0000","site":"site5"},{"title":"Other unique title","link":"anydomainlinkto-other-unique-title/","image":"anyotherurlto/img/other-title.jpg","date":"Mon, 26 Aug 2019 09:18:10 -0000","site":"site3"}];
var result = arr.reduce(function(o, v) {
var key = v.title.replace(/ /g, '').toLowerCase();
if (!o[key]) o[key] = { title: v.title, image: v.image, links: [] };
o[key].links.push({ date: v.date, site: v.site, link: v.link });
return o;
}, {});
console.log( Object.keys(result).map(function(k) { return result[k]; }) );

How to know if the objects exist in postman like below:

How to know if the objects exist in postman like below, i want to check all the parameters. if all of them are returned properly
[
{
"id": "MnnRVEifcngi2",
"givenName": "Witting and Sons",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Fri Jan 05 2018 07:54:09 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 22:25:54 GMT+0000 (UTC)",
"tags": [
"Web",
"Paradigm"
]
},
{
"id": "E7z9UujhROF2L",
"givenName": "Block Group",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Mon Feb 05 2018 11:50:00 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 13:29:35 GMT+0000 (UTC)",
"tags": [
"Brand",
"Web"
]
},
{
"id": "MzbqnzFImpbkf",
"givenName": "Dickinson - Ziemann",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Fri Feb 02 2018 07:00:32 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 18:11:30 GMT+0000 (UTC)",
"tags": [
"Applications"
]
},
{
"id": "3-vqC_QG5Up8r",
"givenName": "Lindgren - Mitchell",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Sun May 06 2018 07:24:02 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 10:10:23 GMT+0000 (UTC)",
"tags": [
"Branding",
"Mobility",
"Functionality"
]
},
{
"id": "8dUhM_0j5vloD",
"givenName": "Schmitt LLC",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Wed Sep 12 2018 01:16:49 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 03:40:15 GMT+0000 (UTC)",
"tags": [
"Accounts",
"Data"
]
},
{
"id": "zl_43QRBDWBnW",
"givenName": "Barton - Bauch",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue Dec 05 2017 14:12:00 GMT+0000 (UTC)",
"updatedAt": "Tue Oct 23 2018 19:36:52 GMT+0000 (UTC)",
"tags": [
"Response",
"Accountability",
"Identity"
]
},
{
"id": "kxgqxbXBS53_2",
"givenName": "Lind Inc",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue May 22 2018 16:37:33 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 06:35:53 GMT+0000 (UTC)",
"tags": [
"Communications",
"Brand"
]
},
{
"id": "xWAIuoDY5icIl",
"givenName": "Gutkowski - Hickle",
"logo": "http://lorempixel.com/640/480/business",
"createdAt": "Tue Jun 19 2018 21:29:59 GMT+0000 (UTC)",
"updatedAt": "Wed Oct 24 2018 14:02:39 GMT+0000 (UTC)",
"tags": [
"Metrics",
"Infrastructure",
"Accounts"
]
}
]
This is what i am trying but getting an error:
const jsonData = pm.response.json();
pm.test('Has data', function() {
pm.expect(jsonData).to.have.property('id');
});
I want to verify - id, givenName, logo, createdAt, UpdatedAt, tags and all are present and want to make a global function so in other tests I can just call it once
To fix your error, try to loop through your array of object i.e jsonData like below. By the way I didn't play with postman global function creation. Hope this link will help you on how to make/use it as global How to Write Global Functions in Postman
pm.test('Has data', () => {
jsonData.forEach(row => {
pm.expect(row).to.have.property('id');
pm.expect(row).to.have.property('givenName');
pm.expect(row).to.have.property('logo');
pm.expect(row).to.have.property('createdAt');
pm.expect(row).to.have.property('updatedAt');
pm.expect(row).to.have.property('tags');
})
});
For checking all parameters in response you should consider using built-in tv4.validate, which stands for Tiny Validator (for v4 JSON Schema) - it validates simple values and complex objects against provided schema.
In your particular case you would do something like this:
const jsonData = pm.response.json();
const schema = {
items: {
type: "object",
properties: {
id: {type: "string"},
givenName: {type: "string"},
logo: {type: "string"},
createdAt: {type: "string"},
updatedAt: {type: "string"},
tags: {type: "array"}
},
required:["id","givenName","logo", "createdAt", "updatedAt", "tags"]
}
};
pm.test('Schema is valid', () => pm.expect(tv4.validate(jsonData, schema)).to.be.true);
More examples of how to use tv4 can be found in its repo.

How to compare a value against an array [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
This is my JSON object and I need to check for a particular id and get the date
[
{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
]
yourVariable = [{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
];
for (keys in yourVariable) {
// supposing id your are looking for is 57eab193
if (yourVariable[keys].id == '57eab193') {
console.log(yourVariable[keys].date)
}
}
You can use the function find to get a specific according to a condition.
This alternative uses the logical operator || just to avoid access error over an undefined value.
var array = [{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
];
var date = (array.find(o => o.id === "440f0cd9") || {}).date;
console.log(date);
date = (array.find(o => o.id === "ele") || {}).date;
console.log(date);

How to get data by month from JSON

I have this JSON:
[{"id":66,"price":56,"start":"Fri, 20 May 2016 00:00:00 +0000","user_id":8},{"id":65,"price":55.5,"start":"Wed, 18 May 2016 00:00:00 +0000","user_id":8},{"id":64,"price":55.5,"start":"Fri, 13 May 2016 00:00:00 +0000","user_id":8},{"id":63,"price":55.5,"start":"Thu, 12 May 2016 00:00:00 +0000","user_id":8},{"id":62,"price":55,"start":"Fri, 22 Apr 2016 00:00:00 +0000","user_id":8},{"id":61,"price":55.5,"start":"Thu, 28 Apr 2016 00:00:00 +0000","user_id":8},{"id":60,"price":54.5,"start":"Thu, 21 Apr 2016 00:00:00 +0000","user_id":8},{"id":59,"price":55,"start":"Wed, 20 Apr 2016 00:00:00 +0000","user_id":8}]
I create html from this data with:
function draw(data) {
$.each(data, function(idx, obj) {
$('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
});
};
but now I need to show only data for current month...
How I can get data from JSON etc. only for April ?
How to filter it only for month I need?
You can use something like indexOf:
var data = [{"id":66,"price":56,"start":"Fri, 20 May 2016 00:00:00 +0000","user_id":8},{"id":65,"price":55.5,"start":"Wed, 18 May 2016 00:00:00 +0000","user_id":8},{"id":64,"price":55.5,"start":"Fri, 13 May 2016 00:00:00 +0000","user_id":8},{"id":63,"price":55.5,"start":"Thu, 12 May 2016 00:00:00 +0000","user_id":8},{"id":62,"price":55,"start":"Fri, 22 Apr 2016 00:00:00 +0000","user_id":8},{"id":61,"price":55.5,"start":"Thu, 28 Apr 2016 00:00:00 +0000","user_id":8},{"id":60,"price":54.5,"start":"Thu, 21 Apr 2016 00:00:00 +0000","user_id":8},{"id":59,"price":55,"start":"Wed, 20 Apr 2016 00:00:00 +0000","user_id":8}];
$.each(data, function(idx, obj) {
if(obj.start.toString().indexOf('Apr') > -1){
$('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bid"></div>
$.each(data, function(idx, obj) {
if(obj.start.toString().indexOf('Apr') > -1){
$('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
}
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
Alternative you can check json start with current month like:
var currentMonth = new Date();//current date
$.each(data, function(idx, obj) {
var d = new Date(obj.start);//json date
//compare json month with current month and if is equal append price
if (d.getMonth() === currentMonth.getMonth()) {
$('#bid').append('<div class="row"><div class="col-md-2"><h5>' + obj.price + '</h5></div></div>');
}
});
var data = [{
"id": 66,
"price": 56,
"start": "Fri, 20 May 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 65,
"price": 55.5,
"start": "Wed, 18 May 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 64,
"price": 55.5,
"start": "Fri, 13 May 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 63,
"price": 55.5,
"start": "Thu, 12 May 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 62,
"price": 55,
"start": "Fri, 22 Apr 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 61,
"price": 55.5,
"start": "Thu, 28 Apr 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 60,
"price": 54.5,
"start": "Thu, 21 Apr 2016 00:00:00 +0000",
"user_id": 8
}, {
"id": 59,
"price": 55,
"start": "Wed, 20 Apr 2016 00:00:00 +0000",
"user_id": 8
}];
var currentMonth = new Date();//current date
$.each(data, function(idx, obj) {
var d = new Date(obj.start);//json date
//compare json month with current month and if is equal append price
if (d.getMonth() === currentMonth.getMonth()) {
$('#bid').append('<div class="row"><div class="col-md-2"><h5>' + obj.price + '</h5></div></div>');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bid"></div>
References
Date.prototype.getMonth()

How to make a new object from selecting some data from existing object in angularjs?

I have a json object as follows.
[{
"id": "1",
"username": "vishnu",
"FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "2",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui of creatwhimsy home page",
"time": "2"
}, {
"id": "3",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
}, {
"id": "4",
"username": "vishnu",
"FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
"selectedProject": "timetracker",
"task": "working on ui of creatwhimsy home page and admin side home page",
"time": "1"
}, {
"id": "5",
"username": "vishnu",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}]
Let the object name be Data. I want to make a new object fro Data, but it should only contain "selectedProject" and "time".
How to do that in angularjs?
You can iterate and create a new object with desire data. For this you can use javascript push() function.
what you need to do is
$scope.log = [];
angular.forEach($scope.names, function(value, key){
$scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
});
Here is JS fiddle: http://jsfiddle.net/kyaqzgo6/4/
A sample code for you:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = [{
"id": "1",
"username": "vishnu",
"FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "2",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui of creatwhimsy home page",
"time": "2"
}, {
"id": "3",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
}, {
"id": "4",
"username": "vishnu",
"FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
"selectedProject": "timetracker",
"task": "working on ui of creatwhimsy home page and admin side home page",
"time": "1"
}, {
"id": "5",
"username": "vishnu",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}];
$scope.log = [];
angular.forEach($scope.names, function(value, key){
$scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
});
console.log($scope.log);
});
</script>
Try something like this:
function SomeFunction() {
function getdata(callback, url) {
var obj= {};
$http.get(url).success(function(response) {
if (response.jsonname[0].length > 0) {
for (var i = 0; i < response.jsonname[0].length; i++) {
obj= {
selectedProject:response.jsonname[0][i].selectedProject,
time:response.jsonname[0][i].time
}
}
} else {
}
}).error(function() {
//something
}).then(function() {
callback(obj);
});;
}
return {
getdata: getdata
}
}

Categories

Resources