I am working on a network graph code that is rendered as json array.
I need to change to code in order to give the array data received from another file.
The code that I have:
var json = [
{
"adjacencies":
[
{
"nodeTo": "A",
"nodeFrom": "B",
"data":
{
"$color": "#557EAA"
}
}
],
"data":
{
"$color": "#909291",
"$type": "circle",
"$dim": 20
},
"id": "B",
"name": "B"
},
{
"adjacencies":
[
{
"nodeTo": "C",
"nodeFrom": "D",
"data":
{
"$color": "#416D9C"
}
}
],
"data":
{
"$color": "#83548B",
"$type": "circle",
"$dim": 20
},
"id": "D",
"name": "D"
},
];
I need to create a for loop that goes from 1 to x, and in every iteration, i add a value in the json array. (A value is composed of "adjacencies", "data", "id", "name")
Note that the nodeTo, nodeFrom, id and name values are stored in another array, so in the loop i can set their values.
Thanks in advance:)
I have tried the .push as follows:
var json = [];
var adjacencies = [];
for (i = 0; i < count; i++) {
var adjacencies = [];
var source = impactsString[i].Source;
var target = impactsString[i].Target;
var number = impactsString[i].NumberOfImpacts;
adjacencies.push({ nodeTo: target, nodeFrom: source });
json.push({ adjacencies: adjacencies, id: source, name: source });
}
But still not working
Use Array.push() as follows:
var json = [
{
"adjacencies":
[
{
"nodeTo": "A",
"nodeFrom": "B",
"data":
{
"$color": "#557EAA"
}
}
],
"data":
{
"$color": "#909291",
"$type": "circle",
"$dim": 20
},
"id": "B",
"name": "B"
},
{
"adjacencies":
[
{
"nodeTo": "C",
"nodeFrom": "D",
"data":
{
"$color": "#416D9C"
}
}
],
"data":
{
"$color": "#83548B",
"$type": "circle",
"$dim": 20
},
"id": "D",
"name": "D"
},
];
var i;
for (i = 0; i < 500; i += 1) {
json.push({
"adjacencies":
[
{
"nodeTo": "A",
"nodeFrom": "B",
"data":
{
"$color": "#557EAA"
}
}
],
"data":
{
"$color": "#909291",
"$type": "circle",
"$dim": 20
},
"id": "B",
"name": "B"
});
}
Related
I have a nested array like below. There are about 100 de objects in the array. The de objects also have deg[0] array but most likely I will only have the first index. Now the trick is that the de are subset of deg. Which means each deg can have say 10 de. How can I retrieve the deg and there associated de and map it into a new array like:
newArray = [
deg1: [
{de1},
{de2}
],
deg2: [
{de1},
{de2}
]
]
Here is my nested array. I posted four but the list is over a 100.
{
"name": "Report",
"id": "2YYUEZ6I1r9",
"dse1": [
{
"de1": {
"name": "Number",
"id": "HjMOngg3kuy",
"de1-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "TB",
"id": "2XJB1JO9qX8"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de1": {
"name": "Number of",
"id": "a3dtGETTawy",
"de1av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Tertiary",
"id": "w99RWzXHgtw"
}
]
}
}
]
}
Group array of objects by property (this time a property to be matched by a reg exp) using Array.reduce.
Update: Ignoring missing keys.
var input={name:"Report",id:"2YYUEZ6I1r9",dse1:[{de1:{name:"Number",id:"HjMOngg3kuy","de1-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"TB",id:"2XJB1JO9qX8"}]}},{de2:{name:"Number of",id:"a3dtGETTawy","de2-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de1:{name:"Number of",id:"a3dtGETTawy",de1av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de2:{name:"Number of",id:"a3dtGETTawy",de2av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Tertiary",id:"w99RWzXHgtw"}]}}]}
var reg = new RegExp("^de[0-9]+$");
var reg2 = new RegExp("^deg[0-9]+$");
let obj = input['dse1'].reduce(function(agg, item) {
// do your group by logic below this line
var key = Object.keys(item).find(function(key) {
return key.match(reg) ? key : null;
})
if (key) {
var key2 = Object.keys(item[key]).find(function(key) {
return key.match(reg2) ? key : null;
})
agg[key] = agg[key] || [];
if (key2) {
var to_push = {}
to_push[key2] = item[key][key2]
agg[key].push(to_push)
}
}
// do your group by logic above this line
return agg
}, {});
console.log(obj)
.as-console-wrapper {
max-height: 100% !important;
}
My requirements are very simple: iterate over the array, use the object to determine the selected object, and then push it into the new array, but I don't know how to write it?
Want the result as follows:
[{
key: 'name',
name: "A",
},
{
key: 'note',
name: "C”,
}
]
My code:
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = []
data[0].data.list.map(item => {
data[0].data.show.forEach(prop => {
if (prop !== item.key) {
arr.push({
key: item.key,
name: item.name
})
}
})
})
console.log(arr);
You could check the key if it is not includes in the show property and then push the object.
var data = [{ data: { list: [{ name: "A", key: "name" }, { name: "B", key: "title" }, { name: "C", key: "note" }, { name: "D", key: "desc" }], show: ["title", "desc"] } }],
arr = [];
data[0].data.list.map(({ key, name }) => {
if (!data[0].data.show.includes(key)) {
arr.push({ key, name });
}
});
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try
let d=data[0].data;
let arr= d.list.filter(x=> !d.show.includes(x.key));
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
let d=data[0].data;
let arr= d.list.filter(x=> !d.show.includes(x.key));
console.log(arr);
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = []
data[0].data.list.forEach(keyValue=> !data[0].data.show.includes(keyValue.key) ? arr.push(keyValue): null)
console.log(arr);
var data = [{
"data": {
"list": [{
"name": "A",
"key": "name",
}, {
"name": "B",
"key": "title",
}, {
"name": "C",
"key": "note",
}, {
"name": "D",
"key": "desc",
}],
"show": [
"title",
"desc"
]
}
}]
var arr = data[0].data.list.filter(item => data[0].data.show.every(props => props !== item.key))
console.log(arr);
I am trying to count the values a json array.
I want to count the number of id's in the data array of "sierra" if "beta = b". Hence checking the value of data[].beta against the environment variable ("B") set to value 'b'.
The issue here is I do not have "sierra" in every iteration of data[].
{
"data": [{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "a"
},
{
"type": "alphabet",
"id": "b"
}
]
}
}
},
{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"bravo": {
"data": [
{
"type": "number",
"id": "1"
},
{
"type": "number",
"id": "2"
}
]
}
}
},
{
"alpha": "x",
"beta": "y",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "c"
},
{
"type": "alphabet",
"id": "d"
}
]
}
}
}]
}
Above json is the response body I see in postman. "loop" is the count of my "for" loop.
EDIT 1:
I've tried this:
1. pm.response.json().data[loop].gamma.sierra.data().id).size()
2. for(var loop =0; loop < pm.response.json().data.length; loop++)
{
if(pm.response.json().data[loop].beta===pm.variables.get("B"))
{
pm.response.json().data.map((item, loop) => {
if(item.gamma){ // check if gamma key is present
console.log(item.gamma.sierra.filter(data =>data.id
).length); //
}
});
result=true;
break;
}
}
pm.expect(true).to.eql(result);
Expected: 2
Actual: TypeError: Cannot read property 'sierra' of undefined
Actual: item.relationships.apps.filter is not a function
You could take a dynamic apporach and hand over the key of the object where you like to count a certain inner key.
function count(object, key, subKey) {
const noObject = o => !o || typeof o !== 'object';
function subCount(object) {
if (noObject(object)) return 0;
if (subKey in object) return 1;
return Object.values(object).reduce((s, o) => s + subCount(o), 0);
}
if (noObject(object)) return 0;
if (key in object) return subCount(object[key]);
return Object.values(object).reduce((s, o) => s + count(o, key, subKey), 0);
}
var data = { data: [{ alpha: "a", beta: "b", delta: { cat: "dog" }, gamma: { sierra: { data: [{ type: "alphabet", id: "a" }, { type: "alphabet", id: "b" }] } } }] };
console.log(count(data, 'sierra', 'id')); // 2
You can access it like this. If you have multiple data records you can use each loop to calculate as well.
a = {
"data": [
{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "a"
},
{
"type": "alphabet",
"id": "b"
}
]
}
}
}
]
}
console.log(a.data[0].gamma.sierra.data.length);
You can use below code:
pm.response.json().data[0].gamma.sierra.data.filter( d => d.id ).length
Hope it helps.
pm.response.json().data.map((item, loop) => {
if(item.beta === "b" && item.gamma){ // check if gamma key is present
console.log(item.gamma.sierra.data.filter(data => data.id).length); //
}
});
Jsfiddle
I have result of an array which contain number of objects and need to split with the help of categories. when categories array value of sum reach in 15 then this array will be more split like below result.
Helps are definitely appreciated
Array
[Object { name="A", categories=[2]}, Object { name="B", categories=[3]}, Object { name="C", categories=[1]}, Object { name="D", categories=[1]}, Object { name="E", categories=[1]}, Object { name="F", categories=[3]}, Object { name="G", categories=[1]}, Object { name="H", categories=[1]}, Object { name="I", categories=[1]}, Object { name="J", categories=[3]}, Object { name="K", categories=[2]}, Object { name="L", categories=[2]}, Object { name="M", categories=[2]}]
More Detail
[{"name":"A","categories":["543","301"]},{"name":"B","categories":["100","120","390"]},{"name":"C","categories":["453"]},{"name":"D","categories":["112"]},{"name":"E","categories":["542"]},{"name":"F","categories":["534","545","547"]},{"name":"G","categories":[" 535"]},{"name":"H","categories":["390"]},{"name":"I","categories":["528"]},{"name":"J","categories":["101","492"," 320"]},{"name":"K","categories":["201","210"]},{"name":"L","categories":["102","573"]},{"name":"M","categories":["211","220"]}]
Desired Result
[[{"name":"A","categories":["543","301"]},{"name":"B","categories":["100","120","390"]},{"name":"C","categories":["453"]},{"name":"D","categories":["112"]},{"name":"E","categories":["542"]},{"name":"F","categories":["534","545","547"]},{"name":"G","categories":[" 535"]},{"name":"H","categories":["390"]},{"name":"I","categories":["528"]},{"name":"J","categories":["101"]}],[{"name":"J","categories":["492"," 320"]},{"name":"K","categories":["201","210"]},{"name":"L","categories":["102","573"]},{"name":"M","categories":["211","220"]}]]
This result part will provide you more help regarding desired result
{"name":"J","categories":["101"]}],[{"name":"J","categories":["492"," 320"]}
A proposal with Array.prototype.reduce() and a temporary object.
var array = [{ "name": "A", "categories": ["543", "301"] }, { "name": "B", "categories": ["100", "120", "390"] }, { "name": "C", "categories": ["453"] }, { "name": "D", "categories": ["112"] }, { "name": "E", "categories": ["542"] }, { "name": "F", "categories": ["534", "545", "547"] }, { "name": "G", "categories": [" 535"] }, { "name": "H", "categories": ["390"] }, { "name": "I", "categories": ["528"] }, { "name": "J", "categories": ["101", "492", "320"] }, { "name": "K", "categories": ["201", "210"] }, { "name": "L", "categories": ["102", "573"] }, { "name": "M", "categories": ["211", "220"] }],
result = array.reduce(function (r, a) {
a.categories.forEach(function (b, i) {
var o = { name: a.name, categories: [] };
r.count++;
if (!r.refA || r.count > 15) {
r.count = 1;
r.refA = [];
r.array.push(r.refA);
i = 0;
}
if (!i) {
r.refA.push(o);
r.refB = o.categories;
}
r.refB.push(b);
});
return r;
}, { array: [], count: 0, refA: undefined, refB: undefined }).array;
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
My sample javascript array format which I get from server:
var json =[
{
"id": "1",
"tagName": [
{
"fruit": "apple"
},
{
"fruit": "watermelon"
}
]
},
{
"id": "2",
"tagName": [
{
"fruit": "orange"
},
{
"fruit": "pineapple"
}
]
},
{
"id": "3",
"tagName": [
{
"fruit": "banana"
},
{
"fruit": "guava"
}
]
}
];
I need to create a javascript function which will generate an array from the above array which will be like this
var json1 = ["1", "2", "3"]
AND
var json1a = [{ "id": "1" }, { "id": "2" }, { "id":"3" }]
All help is sincerely appreciated
Thanks
You simply need to iterate through the array:
var json1 = [];
var json1a = [];
for (var i = 0; i < json.length; i++){
json1.push(json[i].id);
json1a.push({id: json[i].id});
}
If I may advise, you IDs should be Numbers, instead of strings. In this case, you would use:
json1.push(json[i].id);
json1a.push({"id", json[i].id});
Here is an updated fiddle.