How to insert object of arrays into an object- javascript - javascript

This is the data I'm trying to create for my POST method:
{
"main": {
"b": [{
"field": "ss",
"value": "30",
"operator": "LT"
}],
"a": [{
"field": "tttt",
"value": "s",
"operator": "IN"
}
]
}
}
The below fiddle has the code that I use to get this JSON structure.
http://jsfiddle.net/dz4cq0sj/14/
While looping through an array, it takes only the last object that was created and does not push any previous objects, like in this case I have:
obj.criteria[a,b] //whereaandbare objects, so it takes only the b objects details.
{
"main": {
"b": [{
"field": "ss",
"value": "30",
"operator": "LT"
}],
"a": []
}
}
Any idea how to fix this?
Thanks!

Related

Trouble handling nested JSON data

I have a JSON data and i'm having trouble with one nested object. You can see there is two metadata object, my aim is to get image link.
obj.metadata.url
is working, but this isn't work.
obj.metadata.metadata.image
what am i missing here?
{
"tokenId": "0",
"metadata": {
"url": "https://gateway.pinata.cloud/ipfs/QmSnTNGbhD/0",
"metadata": {
"name": "Illuminati #0",
"image": "https://gateway.pinata.cloud/ipfs/QmW8pAhkNr/5499.png",
"attributes": [
{
"trait_type": "Background",
"value": "Stained Glass"
},
{
"trait_type": "Frames",
"value": "Stained Glass"
}
]
},
"tokenId": "0"
}
}
From your question it seems that you are dealing with JSON data instead of JavaScript objects. In order to get the attributes of JSON, you will first need to parse the JSON using JSON.parse(), then you can use . to get the properties you want.
data = {
"tokenId": "0",
"metadata": {
"url": "https://gateway.pinata.cloud/ipfs/QmSnTNGbhD/0",
"metadata": {
"name": "Illuminati #0",
"image": "https://gateway.pinata.cloud/ipfs/QmW8pAhkNr/5499.png",
"attributes": [
{
"trait_type": "Background",
"value": "Stained Glass"
},
{
"trait_type": "Frames",
"value": "Stained Glass"
}
]
},
"tokenId": "0"
}
};
obj = JSON.parse(data);
console.log(obj.metadata.url)
// 'https://gateway.pinata.cloud/ipfs/QmSnTNGbhD/0'
console.log(obj.metadata.metadata.image);
// 'https://gateway.pinata.cloud/ipfs/QmW8pAhkNr/5499.png'

Getting sum from nested array objects with Lodash?

Currenly using Angular with lodash to do some additional usability but currently hitting a roadblock.
I have the following arrays:
{
"Result": [
{
"Name": "marketeerBoston",
"Label": "Week25",
"Total": 251200,
"Specific": [
{
"Label": "3",
"Value": 25,
},
{
"Label": "4",
"Value": 250,
}
]
},
{
"Name": "marketeerJersey",
"Label": "Week25",
"Total": 776090,
"Specific": [
{
"Label": "1",
"Value": 32,
},
{
"Label": "2",
"Value": 37,
}
]
},
],
}
I really need to have the Value summed up from both array objects (so I got 344).
How to achieve that with lodash?
With lodash, you can use nested _.sumBy() calls:
const data = {"Result":[{"Name":"marketeerBoston","Label":"Week25","Total":251200,"Specific":[{"Label":"3","Value":25},{"Label":"4","Value":250}]},{"Name":"marketeerJersey","Label":"Week25","Total":776090,"Specific":[{"Label":"1","Value":32},{"Label":"2","Value":37}]}]}
const result = _.sumBy(data.Result, obj => _.sumBy(obj.Specific, 'Value'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>

Reverse an array of object?

How can i reverse this array using react native ? i tried myarray.reverse(); but I get below error message.
TypeError: undefined is not an object (evaluating 'myarray.reverse')
const myarray =
Array [
Object {
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": Object {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
Object {
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": Object {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]
try to remove Array and Object from the JSON. Works perfect for me.
const myarray = [{
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
{
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]
myarray.reverse()
Is the pasted code what you are actually getting back from the API?
The formatting/syntax is not correct. As others have stated you shouldn't have the 'Array' and 'Object' words inline like that. I think you're copying code from a terminal/console that added those words in.
If the response actually looks like this you won't have any problem using myaray.reverse()
const myarray = [{
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
{
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]

JSONPath exclude as separate JSON

I have a complex JSON Structure . I wan't to exclude a part and get the list in two buckets. Say One as "inclusions" and another as "exclusions".
Sample JSON is given below.
{
"Item": {
"Items": [{
"Items": [{
"category": "fruit",
"Item": {
"Items": [{
"Text": "Apple"
},
{
"Text": "Orange"
},
{
"Text": "Grapes"
},
{
"Text": "Guava"
}],
"Types": ["Value",
"Value",
"Value",
"Value"]
},
"Type": "Or"
},
{
"category": "fruit",
"Item": {
"Text": "Pineapple"
},
"Type": "Value"
}],
"Types": ["Constraint",
"Constraint"]
},
{
"category": "fruit",
"Item": {
"Text": "Banana"
},
"Type": "Value"
}],
"Types": ["Not",
"Constraint"]
},
"Type": "And"}
Here we have category fruits where We Apple,Orange ,Grapes and Guava are with "OR" and pineapple with "AND" Type and Banana is with "Not" .Once I Try
Item..Items[?(#.category='fruit')]..Text
It returns me
[ "Banana", "Apple", "Orange", "Grapes", "Guava", "Pineapple"]
But I want List to be
{["Apple", "Orange", "Grapes", "Guava", "Pineapple" ]
},{["Banana"]}
Is there a way in JSON Path to get something of similar structure
That may be not the answer you expected, but I don't think that's possible with JsonPath.
Quote from jayway/JsonPath
When evaluating a path you need to understand the concept of when a path is definite. A path is indefinite if it contains:
.. - a deep scan operator
?() - an expression
[, (, )] - multiple array indexes
Indefinite paths always returns a list (as represented by current JsonProvider).
So your result will always be a list. What you already have is the closest you can get, I think.
That can not be done using JSONPath.

Sort json preserving index

I'm trying to sort the json below, but until now couldn't find the way. I get this json by jQuery ajax with datatype json. How can I apply sort to this keeping the index? Every time i try to sort javascript throws an error telling undefined is not a function. I already know that json or objects don't have sort.
So how can I achieve this? I really need to keep this index.
{
"header": {
"user": "1059",
"authenticated": true,
"isGuest": false,
"time": 1416362117
},
"batches": {
"3503": { // Preserve this index
"user": "1059",
"auction": "1826",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415379600",
"date_end": "1417021200", // Sort by this value
},
"3583": {
"user": "1059",
"auction": "1886",
"auto_value": "0.00",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415376000",
"date_end": "1417017600",
},
}
}
As I wrote and cited in comments - properties do not have an order. Make an array of single-property objects, which you can sort. Something like this:
var data = {
"header": {
"user": "1059",
"authenticated": true,
"isGuest": false,
"time": 1416362117
},
"batches": [ // this is an array of objects, not a single object
{"3503": { // Preserve this index
"user": "1059",
"auction": "1826",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415379600",
"date_end": "1417021200", // Sort by this value
}},
{"3583": {
"user": "1059",
"auction": "1886",
"auto_value": "0.00",
"number": "1",
"published": "1",
"type": "1",
"status": "1",
"date_start": "1415376000",
"date_end": "1417017600",
}}
] // end of array
}
data.batches.sort(function(a, b) {
return a[Object.keys(a)[0]].date_end - b[Object.keys(b)[0]].date_end;
});
console.log(data.batches);
And you can keep those numbers inside of the object containing user, auction and so on for easier access.

Categories