Convert Array of List into my Own Order
Current Output:
[
{ "key": "DG Power Output", "value": "6.00", "unit": "kWh", },
{ "key": "DG Run Time", "value": "5999999952", "unit": "minutes", },
{ "key": "Fuel Level (Before)", "value": "8.00", "unit": "liters", }
]
Convert this into
[
{ "key": "Fuel Level (Before)", "value": "8.00", "unit": "liters", },
{ "key": "DG Run Time", "value": "5999999952", "unit": "minutes", },
{ "key": "DG Power Output", "value": "6.00", "unit": "kWh", }
]
You could take an object for getting th right order and sort by the property which describes the order.
var data = [{ key: "DG Power Output", value: "6.00", unit: "kWh" }, { key: "DG Run Time", value: "5999999952", unit: "minutes" }, { key: "Fuel Level (Before)", value: "8.00", unit: "liters" }],
order = { "Fuel Level (Before)": 1, "DG Run Time": 2, "DG Power Output": 3 };
data.sort(({ key: a }, { key: b }) => order[a] - order[b]);
console.log(data);
The most basic I would go would be by accessing the index, let's say
var a = [
{ "key": "DG Power Output", "value": "6.00", "unit": "kWh", },
{ "key": "DG Run Time", "value": "5999999952", "unit": "minutes", },
{ "key": "Fuel Level (Before)", "value": "8.00", "unit": "liters", }
]
Then,
var b = [a[2], a[1], a[0]];
This would give the output you want but it's very risky and error-prone.
I'm not entirely sure what criteria you want to use to sort this array, but the general approach is to write a function that compares two elements and returns a number less than 0 for the first element to come first, 0 if they are equal, and a number greater than 0 for the second element to come first. You can then pass this function to Array.prototype.sort like this for descending order:
const sorter = (a, b) => {
if (a.key == b.key) return 0; // leave elements in place
if (a.key > b.key) return -1; // a should come before b for descending order
return 1; // b should come before a for descending order
};
const arr = [
{ "key": "DG Power Output", "value": "6.00", "unit": "kWh", },
{ "key": "DG Run Time", "value": "5999999952", "unit": "minutes", },
{ "key": "Fuel Level (Before)", "value": "8.00", "unit": "liters", }
];
console.log(arr.sort(sorter));
Related
Hey I would like to ask if it is possible to find lets say in 10 JSON files which are looking like this:
1.json:
{
"name": "Miami",
"attributes": [
{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
}
2.json:
{
"name": "New York",
"attributes": [
{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
And so on... just different attribute values.
Lets say I want to find only towns with People > 2500 and I'm not even sure if it is possible or do I need to upload the json files to some database perhaps?
Thank you.
const data = [{
"name": "Miami",
"attributes": [{
"att_type": "People",
"value": "1000"
},
{
"att_type": "Cars",
"value": 300
}
]
},
{
"name": "New York",
"attributes": [{
"att_type": "People",
"value": "5000"
},
{
"att_type": "Cars",
"value": 900
}
]
}
]
const result = data
// find cities with attribute `People` with value greater than 2500
.filter(d => +d.attributes.find(attr => attr.att_type === 'People').value > 2500)
// get name of the city
.map(d => d.name)
console.log(result)
I understand that I can extract for example the currency ("EUR") with order.unitPricePaid.currency, but how do I extract for example the tax number ("GB08713331")?
Below is the data I have:
{
"unitPricePaid": {
"currency": "EUR",
"value": "59.00"
},
"formSubmission": [
{
"label": "User",
"value": "Creatively"
}, {
"label": "Tax number",
"value": "GB08713331"
}
]
}
order.formSubmission[1].value
What this does is it looks at the "formSubmission" as an array. So, you can access each element how it is. Since "label: Tax Number" and "value: GB08713331" are in the second element of the array, you use "formSubmission[1]"
You can get the tax number using Array.prototype.find on the formSubmission property.
const data = {
"unitPricePaid": {
"currency": "EUR",
"value": "59.00"
},
"formSubmission": [{
"label": "User",
"value": "Creatively"
}, {
"label": "Tax number",
"value": "GB08713331"
}]
};
const taxNumber = data.formSubmission
.find(field => field.label === 'Tax number').value;
console.log(`Tax number = "${taxNumber}"`);
Assuming this object:
var order = { "unitPricePaid": { "currency": "EUR", "value": "59.00" }, "formSubmission": [{ "label": "User", "value": "Creatively" }, { "label": "Tax number", "value": "GB08713331" }] }
the path to that value would be order.formSubmission[1].value. The [1] means we are accessing the element at index 1 of the array (index 0 would be the first element).
having an array with objects and inside an options array how do I filter the inner array of objects by key value?
Here is the following example:
let test = [{
"options": [{
"label": "Audi",
"value": 10
},
{
"label": "BMW",
"value": 18
},
{
"label": "Mercedes Benz",
"value": 116
},
{
"label": "VW",
"value": 184
}
],
"label": "test1"
},
{
"options": [{
"label": "Adler",
"value": 3664
},
{
"label": "Alfa Romeo",
"value": 3
},
{
"label": "Alpine",
"value": 4
}
],
"label": "test2"
}
]
how do I get back the object:
{
"label": "Audi",
"value": 10
}
if I filter with keyword Audi
return label.toLowerCase().includes(inputValue.toLowerCase());
I tried with the following
test.map((k) => {
res = k.options.filter((j) => {
inputValue.toLowerCase();
if (j.label.toLowerCase().includes(inputValue.toLowerCase())) {
return j;
}
});
});
You need to return the result of filter(), not just assign it to a variable, so that map() will return the results.
let test = [{
"options": [{
"label": "Audi",
"value": 10
},
{
"label": "BMW",
"value": 18
},
{
"label": "Mercedes Benz",
"value": 116
},
{
"label": "VW",
"value": 184
}
],
"label": "test1"
},
{
"options": [{
"label": "Adler",
"value": 3664
},
{
"label": "Alfa Romeo",
"value": 3
},
{
"label": "Alpine",
"value": 4
}
],
"label": "test2"
}
]
let inputValue = "audi";
let search = inputValue.toLowerCase();
let result = test.map(k => k.options.filter(j => j.label.toLowerCase().includes(search)));
console.log(result);
This will return all options matching the search query :
function find(array, query) {
return array.reduce((prev, current) => prev.concat(current.options), []).filter(item => item.label.includes(query))
}
find(test, 'Audi')
Use flatMap() followed by filter():
let test=[{options:[{label:"Audi",value:10},{label:"BMW",value:18},{label:"Mercedes Benz",value:116},{label:"VW",value:184}],label:"test1"},{options:[{label:"Adler",value:3664},{label:"Alfa Romeo",value:3},{label:"Alpine",value:4}],label:"test2"}]
let result = test.flatMap(el => {
return el.options.filter(car => car.label == "Audi")
})[0]
console.log(result)
You need to go two levels deep when traversing your array.
function filterArray(needle, haystack) {
return haystack
.map(h => h.options)
.flatMap(h => h )
.filter(h => h.label.toLowerCase().includes(needle.toLowerCase()));
CodeSandbox
This might gile your answer:
console.log(test[0].options[0]);
I have a single line chart, with dates on the X axis. After a certain date,
I would like the line to be a different color. Is this possible using ngx-charts?
Let us assume the date after which you want to change the color as T.
Now you can divide the series into 2 parts
The from start date to T
From T to end date.
And now you can plot the graph using different color for different series
The following data will generate the desired graph.
var data = [
{
"name": "Current",
"series": [
{
"value": 5599,
"name": "2016-09-20T01:04:28.176Z"
},
{
"value": 6247,
"name": "2016-09-20T12:51:24.713Z"
},
{
"value": 4283,
"name": "2016-09-18T15:42:04.800Z"
},
{
"value": 2643,
"name": "2016-09-13T20:10:53.904Z"
},
{
"value": 4105,
"name": "2016-09-18T06:15:10.845Z"
},
{
"name": "2016-09-18T13:08:42.085Z",
"value": 4401
},
{
"name": "2016-09-20T01:04:28.176Z",
"value": 3443
}
]
},
{
"name": "Future",
"series": [
{
"value": 3443,
"name": "2016-09-20T01:04:28.176Z"
},
{
"value": 2604,
"name": "2016-09-20T12:51:24.713Z"
},
{
"value": 2158,
"name": "2016-09-18T15:42:04.800Z"
},
{
"value": 5519,
"name": "2016-09-13T20:10:53.904Z"
},
{
"value": 4532,
"name": "2016-09-18T06:15:10.845Z"
},
{
"name": "2016-09-18T13:08:42.085Z",
"value": 2474
}
]
}
]
For Example
I have Object named tempobj and below are tempobj[0] and tempobj[1] sample data.
I want to add extra info like name and status this object
tempobj ["info"]["name"] = "title";
tempobj ["info"]["id"] = "23243";
But when i do stringify , Jquery is ignoring this value...how can i add data like this to this kind of structure.
[
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
tempobj is an array ([]).
When you try to set some values with:
tempobj["info"]["name"] = "title";
tempobj["info"]["id"] = "23243";
you treat it like an object ({}).
If you want to add some data to your tempobj, you have to change its structure like this for example:
{
"info": {
"name": "title",
"id": "23243"
},
"items": [
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
]
}
Here is a sample:
var tempobj = {
items: [
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
]
}
tempobj.info = {
name: 'title',
id: '23243'
};
// Another way:
//tempobj.info = {};
//tempobj.info.name = 'title';
//tempobj.info.id = '23243';
console.log(JSON.stringify(tempobj));
It sounds like you're doing something like this:
// Create an array
var a = [];
// Add an entry to it
a[0] = "I'm an array entry";
// Add a non-entry property to it
a.foo = "bar";
// Convert to JSON
var json = JSON.stringify(a);
...and finding that the string doesn't have the foo property.
That's correct. JSON's arrays don't support arbitrary non-entry properties the way JavaScript's arrays do, and so they get silently dropped during serialization (the same way properties with the value undefined or referring to functions do).
The answer is not to do that if you need to go through JSON.
But, the structure you quoted later in your question doesn't do that, and can readily be created like this:
var data = [
[
{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
},
{
"name": "snack ",
"value": "pecan pie butter",
"check": 0
},
{
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}
],
[
{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
},
{
"name": "snack ",
"value": "Smoked Salmon nori roll "
},
{
"name": "dessert",
"value": "Apple muffins"
}
]
];
var json = JSON.stringify(data);
That structure is (from the outside in): An array containing two entries, each of which is another array; within each array, you have a series of objects, which have properties.
I don't know why you want to have the two arrays inside the outermost array, but that's what the structure showed.
If you meant just a single array, that would look like this if you were creating it all at once:
var data = [
{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
},
{
"name": "snack ",
"value": "pecan pie butter",
"check": 0
},
{
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
},
{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
},
{
"name": "snack ",
"value": "Smoked Salmon nori roll "
},
{
"name": "dessert",
"value": "Apple muffins"
}
];
var json = JSON.stringify(data);
...or if building it up over time:
var data = [];
data.push({
name: "Breakfast",
value: "buffalo strip ",
check: 0
});
data.push({
name: "snack ",
value: "pecan pie butter",
check: 0
});
// ...
var json = JSON.stringify(data);