I have an array of two objects which has another array of objects, i want the count of all objects of property "value" in the following sample.
var data = [
{
"x": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}, {
"y": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}
];
I have used the following logic :-
let _data = data.RESULT;
console.log(JSON.stringify(_data));
_data.forEach(element => {
this.someObj = element;
});
The expected result should be the length of values property, i.e. 4
If you want to find only length of "values" array then you need to do this
data = [
{
"x": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}, {
"y": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}
]
length = 0;
constructor() {
this.data.forEach((d) => {
length = length + d.values.length;
});
console.log('length', length);
}
https://stackblitz.com/edit/angular-3daqcy?file=src%2Fapp%2Fapp.component.ts
Created a function that adds the lengths of the values array
var arr = [
{
"x": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}, {
"y": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}
]
function getKeysLength(arr){
var count = 0;
arr.forEach((val)=>{count= count+ val.values.length})
return count
}
var ans = getKeysLength(arr);
console.log("ans",ans);
If you want to do it in a single line then you can do it with map and reduce,map creates a new array with the elements as the length of the values property and reduce calculates the sum of the new array elements . Which gives you the sum of the length of all values property arrays.
Here is the code below -
let data = [
{
"x": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}, {
"y": "123",
"values": [
{
"a": "1"
},
{
"b": "2"
}
]
}
];
let totalLength = data.map(x => x.values.length).reduce((a,b) => a+b,0);
console.log(temparr);
Here is a working Stackblitz with Angular 6
https://stackblitz.com/edit/hello-angular-6-ewa5tb?file=src/app/app.component.ts
Related
The array in this order:
I want to create a new array as:
You can apply map() function on your data to get your desired output like you posted above. According to question, a possible solution would be as below
const data = [{
"date": "2020-01-01",
"point": {
"a": "1",
"b": "2"
}
},
{
"date": "2020-02-01",
"point": {
"a": "3",
"b": "4"
}
},
{
"date": "2020-03-01",
"point": {
"a": "5",
"b": "6"
}
},
{
"date": "2020-04-01",
"point": {
"a": "7",
"b": "8"
}
}
];
const res = data.map((_, index) => {
return {
series: {
data: data.slice(0, index + 1).map(i => [Number(i.point.a), Number(i.point.b)])
}
}
});
console.log(res);
You could try something like this:
resultArray = yourarray.map(function(data){
return {
series:{
"data":[data.points.a,data.points.b]
}
}
})
I believe this will get you what you want:
const orig = [
{
"date": "2020-01-01",
"point": {
"a": "1",
"b": "2"
}
},
{
"date": "2020-02-01",
"point": {
"a": "3",
"b": "4"
}
},
{
"date": "2020-03-01",
"point": {
"a": "5",
"b": "6"
}
},
{
"date": "2020-04-01",
"point": {
"a": "7",
"b": "8"
}
}
];
// Make the new array.
const points = [];
const newArray = orig.map( x => {
points.push([x.point.a, x.point.b]);
return {
"series": {
"data": points.slice(0)
}
}
});
// Log the new array.
console.log(newArray);
Trying to map array of objects with values nested in child objects structure like:
const objs = [{
"B": {
"value": 1,
},
"D": {
"value": "45"
},
"E": {
"value": "234"
},
"A": {
"value": "543"
},
"C": {
"value": "250"
}
},...]
to the structure like:
[
{ name: 'B', value: 1 },
{ name: 'D', value: '45' },
{ name: 'E', value: '234' },
{ name: 'A', value: '543' },
{ name: 'C', value: '250' }
]
and the result of the mapping is undefined
const mapped = objs.map((key, index) => {
Object.keys(key).map(el => ({
name: el
}))
})
Example: Stackblitz
You are missing return statement and value property definition.
Besides you may want to use flatMap instead of map in order to avoid a nested array in the result:
const objs = [{
"B": {
"value": 1,
},
"D": {
"value": "45"
},
"E": {
"value": "234"
},
"A": {
"value": "543"
},
"C": {
"value": "250"
}
}]
const mapped = objs.flatMap((key, index) => {
return Object.keys(key).map(el => ({
name: el,
value: key[el].value
}))
})
console.log(mapped)
You should operate on objs[0], not objs, because it is an array of one object, not array of objects.
let array = []
for(let object in objs[0]){
array.push({
"name": object,
"value": objs[0][object].value
})
}
return is missing in Object.keys. As well instead of Object.keys use Object.entries to get key and value.
const objs = [{
"B": {
"value": 1,
},
"D": {
"value": "45"
},
"E": {
"value": "234"
},
"A": {
"value": "543"
},
"C": {
"value": "250"
}
}];
const mapped = objs.map((key, _) => {
return Object.entries((key)).map(([name, {
value
}]) => ({
name,
value
}))
}).flat();
console.log(mapped);
i want to merge some value object with same key and value, in this case "field_template_id": 2 is duplicate but have different value.
json data :
{
"id": "c2dec94f",
"data": [
{
"field_template_id": 1,
"value": "111"
},
{
"field_template_id": 2,
"value": 222
},
{
"field_template_id": 2,
"value": 444
},
{
"field_template_id": 3,
"value": [
333
]
}
]
}
i expected to be like this, there still "field_template_id": 2 but the value is array.
expected json :
{
"id": "c2dec94f",
"data": [
{
"field_template_id": 1,
"value": "111"
},
{
"field_template_id": 2,
"value": [
222, 444
]
},
{
"field_template_id": 3,
"value": [
333
]
}
]
}
Thanks in advance
const json = {
"id": "c2dec94f",
"data": [
{
"field_template_id": 1,
"value": "111"
},
{
"field_template_id": 2,
"value": 222
},
{
"field_template_id": 3,
"value": [
333
]
},
{
"field_template_id": 2,
"value": 444
}
]
}
const data = json.data
let obj = {}
let arr = []
data.forEach(item => {
if (obj[item.field_template_id]) {
arr.some((val, key) => {
const newItem = arr[key]
if (val.field_template_id === item.field_template_id) {
if (Array.isArray(newItem.value) && Array.isArray(item.value)) {
newItem.value = newItem.value.concat(item.value)
} else if (Array.isArray(newItem.value)) {
newItem.value.push(item.value)
} else if (Array.isArray(item.value)) {
item.value.unshift(newItem.value)
} else {
const result = []
result.push(newItem.value)
result.push(item.value)
newItem.value = result
}
return true
} else {
return false
}
})
} else {
obj[item.field_template_id] = true
arr.push(item)
}
})
const result = {
id: json.id,
data: arr
}
console.log(result)
I want to change the below JSON Data to Expected Format.
JSON DATA:
[
{
"A": {
"X": "P"
},
"B": {
"X": "Q"
},
"C": {
"X": "R"
}
}
]
Expected Format:
[
{
"A": "P",
"B": "Q",
"C": "R"
}
]
Thanks in advance. :)
Try this. You can get the keys of the each item and then map to the correspond structure of object.
const json = [
{
"A":{
"X":"P"
},
"B":{
"X":"Q"
},
"C":{
"X":"R"
}
}
];
const expectedJSON = json.map(item => {
const obj = {};
Object.keys(item).forEach(key => obj[key] = item[key].X);
return obj;
})
console.log(expectedJSON);
In case X property has different names for each object you can use this method.
const json = {
"A": { "X": "P" },
"B": { "X": "Q" },
"C": { "X": "R" }
};
for (let prop in json) {
for (let item in json[prop]) {
json[prop] = json[prop][item];
}
}
console.log(json);
I have an object that looks like this:
{
"KeyValueOfstringstring": [
{
"Key": "FET",
"Value": "123"
},
{
"Key": "FFS2",
"Value": "Z"
},
{
"Key": "LoadIndex",
"Value": "91"
},
{
"Key": "Ply",
"Value": "B"
}
]
}
and i want it to look like this:
{
"KeyValueOfstringstring": [
{
"FET": 123,
"FFS2": "Z",
"LoadIndex": "91",
"Ply": "B"
}
]
}
Has anyone done this before or has any idea how this could be accomplished? Unfortunately this is the response from a WS and thus have to work with it.
You can do it with a regular for loop:
var result = {};
for (var i = 0; i < object.array_with_long_name.length; i++) {
var o = object.array_with_long_name[i];
result[o.Key] = o.Value;
}