I have array=
[{
"2022-12-06": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}]
},
{
"2022-09-08": [{
"student": "20",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "300",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
},
{
"date20221208": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
}
]
I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false
I want find a date and return value date is exits or not
for eg.
i find 2022-12-06 if array have this value then true if not then false
First of all this question is not about react-native. This is about basic javascript.
You can use Array.Prototype.find() method.
const arr = [{
"2022-12-06": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}]
},
{
"2022-09-08": [{
"student": "20",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "300",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
},
{
"date20221208": [{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
},
{
"student": "10",
"duration": "00:00:07",
"intervals": "1"
}
]
}
]
const checkArray = (str) => {
const x = arr.find(item => item.hasOwnProperty(str))
return x ? true:false
// or return object.If has not return undefined
// return x
}
console.log(checkArray("2022-12-06"))
you can use the Array.prototype.find() method to search for an object in an array. This method returns the first element in the array that satisfies the provided testing function.
For example, if you have an array of objects and you want to find an object with a specific date property, you could use the find() method like this:
const array = [{date: '2022-12-06', name: 'John'}, {date: '2020-02-05', name: 'Jane'}, {date: '1990-04-07', name: 'Bob'}];
const object = array.find(obj => obj.date === '2022-12-06');
console.log(object); // {date: '2022-12-06', name: 'John'}
Related
I'm getting data in an specific way from an API and I have to convert it to a cleaner version of it.
What I get from the API is a JSON like this (you can see that there is some information duplicated as for the first fields but the investor is different).
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "",
"gender": ""
},
"inamount": "",
"ratio": ""
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
}
]
}
I need to merge the investors and eliminate the duplicated information, the the expected result will be
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1555",
"gender": "01"
},
"inamount": "2000000",
"ratio": "15"
}
]
}
]
}
I'd try a couple of JOLTS and I got to merge the fields but not eliminate the duplicates.
You can start with grouping by id values such as
[
{
// group by "id" values to create separate objects
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "#(1,id).&",
"investors": {
"*": {
"*": {
"#": "#(4,id).&3[&4].&" // &3 -> going 3 levels up to grab literal "investors", [&4] -> going 4 levels up the tree in order to reach the indexes of "clubhouse" array, & -> replicate the leaf node values for the current key-value pair
}
}
}
}
}
}
},
{
// get rid of "null" values
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
},
{
// pick only the first components from the repeated values populated within the arrays
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE",
"investors": "MANY"
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": ""
}
}
]
I previously have this issue of merging data into another one to avoid duplicates and make a cleaner version of the JSON. I got a solution in here that worked like a charm for a while but after I got more information arrayed inside the JSON things got a little bit tricky.
I have this array:
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "1"
},
{
"nationalityid": "2"
},
{
"nationalityid": "3"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "1"
},
{
"nationalityid": "2"
},
{
"nationalityid": "3"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "3"
},
{
"nationalityid": "4"
},
{
"nationalityid": "5"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"nationalities": [
{
"nationalityid": "3"
},
{
"nationalityid": "4"
},
{
"nationalityid": "5"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "",
"gender": ""
},
"inamount": "",
"ratio": ""
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "3"
},
{
"nationalityid": "4"
},
{
"nationalityid": "5"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
}
]
}
I was using this JOLT but it doesnt work with the nationalities,since it loses the array they are in.
[
{
// group by "id" values to create separate objects
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "#(1,id).&",
"investors": {
"*": {
"*": {
"#": "#(4,id).&3[&4].&" // &3 -> going 3 levels up to grab literal "investors", [&4] -> going 4 levels up the tree in order to reach the indexes of "clubhouse" array, & -> replicate the leaf node values for the current key-value pair
}
}
}
}
}
}
},
{
// get rid of "null" values
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
},
{
// pick only the first components from the repeated values populated within the arrays
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE",
"investors": "MANY"
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": ""
}
}
]
What I need to get is something like this:
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "1"
},
{
"nationalityid": "2"
},
{
"nationalityid": "3"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"nationalities": [
{
"nationalityid": "3"
},
{
"nationalityid": "4"
},
{
"nationalityid": "5"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"nationalities": [
{
"nationalityid": "3"
},
{
"nationalityid": "4"
},
{
"nationalityid": "5"
}
],
"TypeId": "3",
"investors": [
{
"investor": {
"id": "",
"gender": ""
},
"inamount": "",
"ratio": ""
}
]
}
]
}
You can rearrange the first shift transformation by adding a new object tagged "nationalities" which has one level reduced identifiers compared to the already existing object tagged "investors", and the existing cardinality transformation would already pick only the first array among repeated identical "nationalities" arrays if the remaining specs are kept as they are, such as the below one
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "#(1,id).&",
"nationalities": {
"*": {
"#": "#(3,id).&2[&3][]"
}
},
"investors": {
"*": {
"*": {
"#": "#(4,id).&3[&4].&"
}
}
}
}
}
}
},
...
]
Okay so I have a JSON:
{
"Concepts": [
{
"Concept": "1",
"Description": "siopao"
},
{
"Concept": "4",
"Description": "gulaman"
},
{
"Concept": "9",
"Description": "sisig"
},
{
"Concept": "12",
"Description": "noodle"
},
{
"Concept": "15",
"Description": "sisigan"
},
{
"Concept": "16",
"Description": "buko shake"
},
{
"Concept": "17",
"Description": "mango shake"
},
{
"Concept": "19",
"Description": "burger"
},
{
"Concept": "20",
"Description": "sample"
},
{
"Concept": "21",
"Description": "shit"
}
]
}
How do I get only "Description":"siopao"? I'm using AngularJS.
If you want to map the jsonstring to something like:
["siopao", "gulaman", "sisig", "noodle", "sisigan", "buko shake", "mango shake", "burger", "sample", "shit"]
use
var object = JSON.parse(jsonString);
var descriptions = object.Concepts.map(function(c) {
return o.Description;
});
If you only want the one with the description "siopao", add a filter before the map:
var object = JSON.parse(jsonString);
var description = object.Concepts.filter(function(a) {
return a.Description === "siopao";
}).map(function(c) {
return o.Description;
})[0];
How to repeat the below JSON without write for loop using only ng-repeat
$scope.cart = {
"product": [
{
"HH_STYLENUM": "2204-RYP",
"SIZE_QTY": [
{ "QTY": "11", "SIZE": "XS" },
{ "QTY": "11", "SIZE": "XL" },
{ "QTY": "111", "SIZE": "S" },
{ "QTY": "111", "SIZE": "M" },
{ "QTY": "111", "SIZE": "L" }
],
"HH_CUSTID": "2919",
"HH_ADDCART_UNITPRICE": "11.5",
"HH_ADDCART_NO": "188",
"HH_ADDCART_DATE": "2014-12-10 00:25:07.0",
"HH_ADDCART_STATUS": "CART",
"HH_COLORID": "15991"
},
{
"HH_STYLENUM": "2204-HHSH",
"SIZE_QTY": [
{ "QTY": "01", "SIZE": "XS" },
{ "QTY": "03", "SIZE": "XL" },
{ "QTY": "104", "SIZE": "S" },
{ "QTY": "51", "SIZE": "M" },
{ "QTY": "31", "SIZE": "L" }
],
"HH_CUSTID": "2919",
"HH_ADDCART_UNITPRICE": "11.5",
"HH_ADDCART_NO": "188",
"HH_ADDCART_DATE": "2014-12-10 00:25:07.0",
"HH_ADDCART_STATUS": "CART",
"HH_COLORID": "15991"
}
]
};
Based on your comments... you could use two ng-repeats. One to loop over cart.product and within that ng-repeat, another ng-repeat to loop over SIZE_QTY
<div ng-repeat="product in cart.product">
<p ng-repeat="size in product.SIZE_QTY">{{size.SIZE}}</p>
Is the parsing way wrong? I'm trying to get weather data
function Hello($scope, $http) {
$http
.jsonp('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=MYKEY&callback=JSON_CALLBACK')
.success(function(data) {
var datais = JSON.stringify(data);
console.log("Datais::"+datais);
console.log("Weather::"+datais["weather"]);
})
.error(function(data){
alert("Error");
});
}
Output:
Datais::{
"data": {
"current_condition": [{
"cloudcover": "25",
"humidity": "76",
"observation_time": "09:13 AM",
"precipMM": "0.0",
"pressure": "992",
"temp_C": "9",
"temp_F": "48",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [{
"value": "Sunny"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}],
"winddir16Point": "SSW",
"winddirDegree": "200",
"windspeedKmph": "13",
"windspeedMiles": "8"
}],
"request": [{
"query": "London, United Kingdom",
"type": "City"
}],
"weather": [{
"date": "2014-01-16",
"precipMM": "1.6",
"tempMaxC": "11",
"tempMaxF": "52",
"tempMinC": "5",
"tempMinF": "41",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "S",
"winddirDegree": "191",
"winddirection": "S",
"windspeedKmph": "30",
"windspeedMiles": "19"
}, {
"date": "2014-01-17",
"precipMM": "1.3",
"tempMaxC": "10",
"tempMaxF": "50",
"tempMinC": "5",
"tempMinF": "42",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "SSW",
"winddirDegree": "202",
"winddirection": "SSW",
"windspeedKmph": "27",
"windspeedMiles": "17"
}, {
"date": "2014-01-18",
"precipMM": "2.7",
"tempMaxC": "10",
"tempMaxF": "49",
"tempMinC": "4",
"tempMinF": "39",
"weatherCode": "266",
"weatherDesc": [{
"value": "Light drizzle"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
}],
"winddir16Point": "S",
"winddirDegree": "177",
"winddirection": "S",
"windspeedKmph": "23",
"windspeedMiles": "15"
}, {
"date": "2014-01-19",
"precipMM": "1.0",
"tempMaxC": "8",
"tempMaxF": "46",
"tempMinC": "5",
"tempMinF": "41",
"weatherCode": "122",
"weatherDesc": [{
"value": "Overcast"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}],
"winddir16Point": "ESE",
"winddirDegree": "110",
"winddirection": "ESE",
"windspeedKmph": "13",
"windspeedMiles": "8"
}, {
"date": "2014-01-20",
"precipMM": "0.4",
"tempMaxC": "8",
"tempMaxF": "47",
"tempMinC": "1",
"tempMinF": "34",
"weatherCode": "116",
"weatherDesc": [{
"value": "Partly Cloudy"
}],
"weatherIconUrl": [{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}],
"winddir16Point": "NW",
"winddirDegree": "311",
"winddirection": "NW",
"windspeedKmph": "12",
"windspeedMiles": "7"
}]
}
}
Weather::undefined
Here your datais variable is assigned the data.
if you inspect properly the json data returned as you can see in your console.log(datais);
you will be able to access weather property in datais.data.weather.
try this
console.log("Weather::"+datais.data.weather);
You will be able to access weather property.
Further weather property is an array which has weather values from today up to 4 upcoming days. you can access them in a loop
for(i=0; i<datais.data.weather.length; i++){
console.log(datais.data.weather[i]);
}
Or for example access todays weather data:
console.log(datais.data.weather[0].date);
console.log(datais.data.weather[0].tempMaxC);
console.log(datais.data.weather[0].tempMinC);
i htink you need to access the weather this way:
console.log("Weather::"+datais.data.weather);
you may use an onlne json viewer to view the structure of your data: for example: http://jsonviewer.stack.hu/ or take at look at the browser console. firebug, for example, can show the json data as tree.
And you don't need JSON.stringify. Angular does this for you:
.success(function(data) {
console.log(data.data.weather);
})