How to pass list in JavaScript? - javascript

I am trying to dynamically send a list object (as below) in JavaScript.
I am trying to setup a dynamic grid which accepts dynamic column names (instead of hardcoding the columns)
I am trying to create columnmap dynamically that will be used by grid, something like below,
columMap : {
'Header' : [
{ title : "Title", field : "Title" },
{ title : "Created", field : "Created" },
{ title : "Created By", field : "CreatedBy.Account" }
]
I tried with var list={field : 'Name',title:'Name'}.. This works fine for one column but does't work for multiple columns. I tried array too, didn't work.. Anyone has any suggestions?

[] represents an empty array
[1, 2, 3] is an array of three numbers
[ { a: 1 }, { a: 1 } ] is an array of objects
[ 1, "a", { a: 3 } ] an array does not care what type it holds
So...
var list =
[
{field : 'Name',title:'Name'}
];

Sorry, it was my mistake... I forgot to remove [] when I was passing my list object hence it was not able to set the value..
I got it resolved by passing list as below..
var list = [{ field: 'Name',title: 'Name' },{ field:'ContextNamePathRaw',title: 'Ownership Hierarchy'} ];
Thanks for your help!!!
BB

Related

How to get only the specific variation data to be display in react.js

I have created Shopping Cart the product name, image, price, and attributes are created in separate components. I'm getting an issue where I wanted to get only the specific product variation's attribute, but I got all product variation attributes as below image
Above show the product and its details, but the product attributes must show only its specific variation attributes but it shows all its variation attributes. below is the code and the MongoDB database structure of the product.
I don't know where I go wrong, plz help me. thank you in advance.
You can use the array filter method if you want to get a subset of the attributes. For example:
const res = {
data: {
VariationList: {
Attribute: {
Weight: 1,
Length: 2,
Width: 3,
Hook: 4
}
}
}
};
function getProductAttributes(data, filterList = []) {
const entries = Object.entries(data.VariationList.Attribute);
if (!filterList.length) return entries;
return entries.filter((attr) => filterList.includes(attr[0]));
}
let output = getProductAttributes(res.data);
console.log(output); // [ [ 'Weight', 1 ], [ 'Length', 2 ], [ 'Width', 3 ], [ 'Hook', 4 ] ]
output = getProductAttributes(res.data, ['Weight', 'Length']);
console.log(output); // [ [ 'Weight', 1 ], [ 'Length', 2 ] ]

JSON Path: Outputting value plus parent hierarchically

I'm new to JSON Path, so this may be simpler than I expect, if it's even possible.
I have a JSON block that consists of a set of groups, with each group having a set of fields. Each group also has a sequence, as does each field. The field sequence represents order within a group, and the group sequence represents the display order of the group itself.
I generated this programmatically in C# off of a flat list of items (groups and fields) with different sequence values, so I want to validate my output and, by extension, my grouping algorithm.
The JSON block looks (vastly simplified) like this:
{
"groups": [
{
"sequence": 0,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
},
{
"sequence": 2
}
]
},
{
"sequence": 1,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
}
]
},
{
"sequence": 2,
"fields": [
{
"sequence": 0
},
{
"sequence": 1
},
{
"sequence": 2
}
]
}
]
}
I'm trying to validate this with JSON Path. For this, I don't have a tool, so I'm using the jsonpath online evaluator.
What I'm aiming for is output along the lines of this:
'0'
'sequence' => '0'
'fields'...
'0'
'sequence' => '0'
'1'
'sequence' => '1'
'2'
'sequence' => '2'
// etc...
In other words, I'm looking for a JSON path query that can return the sequence of each group plus the sequence of each field in the group, in a hierarchy.
To get the sequence of the groups, I'm using the following. This works fine, and gives me useful output, since groups are the top-level item already:
$.groups[*].sequence
Output:
'0' => "0"
'1' => "1"
'2' => "2"
To get the sequence of the fields in the groups, I'm using the following. The output here is less useful, as it's a flat list that can get difficult to read if I have dozens or more fields spread out across several groups.
$.groups[*].fields[*].sequence
Output:
'0' => "0"
'1' => "1"
'2' => "2"
'3' => "0"
'4' => "1"
'5' => "0"
'6' => "1"
'7' => "2"
So ultimately, the question is this: is there a JSON path query that will let me get the information I need here hierarchically? Or am I using the wrong tool for the job? I suppose I could write a few lines of code in JavaScript that will let me do this if I have to, but it seems like JSON path might be a powerful tool to learn, so if I can do it here I've learned something.
You do not need a variable sequence. When you need sequence, move item from one group to another, sort you can use array in object, and object in array.
Array use to sort, move items, change sequence. Objects to grouping of attributes within a single item
var groups =
[
{"fields": [
{name: 'field1'},
{name: 'field2'},
{name: 'field3'}
],
},
{"fields": [
{name: 'field1'},
{name: 'field2'},
{name: 'field3'}
]
},
{"fields": [
{name: 'field1'},
{name: 'field2'},
{name: 'field3'}
]
}
];
console.log(groups[0].fields); // Array [ Object, Object, Object ]
console.log(groups[2].fields[0].name); // field1
Function splice is the best to change sequence. In same fields, and another fields
Example for change sequence in same fields
var oldIndex = 2, newIndex = 0, item = groups[0].fields.splice(oldIndex, 1)[0];
console.log(item);
groups[0].fields.splice(newIndex, 0, item);
console.log(groups);

Best way to design a model for the store for a array?

I need to import the following into a store but I am confused about the correct model or models I need to create.
here is an example of the JSON that is returned from my server. Basically its an array with 2 items, with an array in each. The field names are different in each.
I suspect I need to have more than one model and have a relationship but I am unsure where to start. Any ideas? Thanks
[
firstItems: [
{
name : "ProductA",
key: "XYXZ",
closed: true
},
{
name : "ProductB",
key: "AAA",
closed: false
}
],
secondItems : [
{
desc : "test2",
misc: "3333",
},
{
desc : "test1",
misc: "123"
}
]
]
What you have is not JSON, your opening and ending [] can become JSON by changing them to {} and then using the following models
Then you can model it as
// Abbreviated definitions of Models, it has changed starting at Ext 5
Ext.define('FirstItem', fields: ['name', 'key', 'closed'])
Ext.define('SecondItem', fields: ['desc', 'misc'])
Ext.define('TopLevel', {
hasMany: [
{model: 'FirstItem', name: 'firstItems'},
{model: 'SecondItem', name: 'secondItems'}
]
})
Use the reader for store's proxy, it will create appropriate model on load.
If you need to load already loaded json into the store use loadRawData but reader you will need in any case.

Knockout Mapping toJSON - cannot ignore nested child

Assume I have a viewModel like below.
var data = {
a: { a1: "a1", a2: "a2" },
b: "b"
};
I would like to ignore a.a1 and b. So my expected JSON is
{"a":{a2:"a2"}}
However, on doing this
var result = ko.mapping.toJSON(data, { ignore: ["a.a1", "b"] })
I am getting result=
{"a":{"a1":"a1","a2":"a2"}}
Knockout mapping is not ignoring a.a1. Is this a bug in the plugin? It correctly ignored 'b' but why not 'a.a1'?
The names found in the ignore array should be the name of the property, regardless of what level it is in the object. You have to use:
{ ignore: [ "a1", "b" ] }
I had similar ignore list where some "ids" have to be suppressed and others left as is. I wanted to expand on the answer so that people using fromJS can see specific ignores do work
var data1 = {
invoice: { id: 'a1', name: 'a2', type: 'a3'},
shipping: "b1",
id:"c1"
};
var resultvm = ko.mapping.fromJS(data1, {'ignore':["invoice.id",
"ship"]}); ko.applyBindings(resultvm);
Will give you an output as below. Notice that only the id for invoice has been ignored.
{"invoice":{"name":"a2","type":"a3"},"id":"c1"}
But toJSON gives
Code:
var result = ko.mapping.toJSON(data1, { ignore: ["invoice.id",
"shipping"] });
Result:
{"invoice":{"id":"a1","name":"a2","type":"a3"},"id":"c1"}
Here is my jsFiddle: https://jsfiddle.net/3b17r0ys/8/

How to pass json data to highcharts series?

I have following json array which is generated at runtime.
Hence the number of name/data pairs varies.
`var sales = { "SalesData" : [
{ "name" : "AllProducts|Canada", "data" :[44936.0,50752.0] },
{ "name" : "AllProducts|Mexico", "data" : [200679.0,226838.0] },
{ "name" : "AllProducts|USA", "data" : [288993.0,289126.0] }
]} `
I want to pass this data to series in highcharts.
This is how I am doing it currently.
series: [
{name:sales.SalesData[0].name,data:sales.SalesData[0].data},
{name:sales.SalesData[1].name,data:sales.SalesData[1].data},
{name:sales.SalesData[2].name,data:sales.SalesData[2].data}
]
But if the number of elements in array are changed then this won't work.
How do I solve this problem ? Demo code will help me.
I have refereed following questions but I was not able to solve the problem.
Dynamically adding to Highcharts
Highcharts series data array
I solved the problem
Changed json array as follows:
var sales = [
{ "name" : "AllProducts123|Canada", "data" :[44936.0,50752.0] },
{ "name" : "AllProducts|Mexico", "data" : [200679.0,226838.0] },
{ "name" : "AllProducts|USA", "data" : [288993.0,289126.0] }
]
Now pass it directly to series in highcharts.
series:sales
Done !!!!!
Instead of constructing the series array manually you could loop through the sales variable data and construct the array. So what ever the number of elements in the sales.SalesData array, all items will be there in the series array
var series = [],
salesData= sales.SalesData;
for (var i=0 i< salesData.length; i++) {
series.push({"name" : key, "data" : sales[key]})
}
This constructed series array is part of the object which you must pass as argument to highcharts method.
var chartdata = {
chart: {type: 'column'},
title: {text: 'Sales Data'},
xAxis: {
categories: ['Category 1','Category 2']
},
yAxis: {
min: 0,
title: {text: 'Sales'}
},
series : []
}
chartdata.series = series;
$('#chart').highcharts(chartdata);
where #chart is the container where you want to display the chart.
you can also refer to the fiddles which are available in their demo pages for each type of chart to know more on how to display a particular type of chart.

Categories

Resources