Enviroment: Visual Studio 2012, MVC4, Razor, Internet Application.
I have a JavaScript array..
var over100 = "#Model.Over100";
var filterarray = [
{
"name": "Condition",
"value": "New",
"paramName": "",
"paramValue": ""
},
{
"name": "FreeShippingOnly",
"value": "false",
"paramName": "Currency",
"paramValue": "USD"
},
];
In case of... if(over100 != null)
I want...
var filterarray = [
{
"name": "Condition",
"value": "New",
"paramName": "",
"paramValue": ""
},
{
"name": "FreeShippingOnly",
"value": "false",
"paramName": "Currency",
"paramValue": "USD"
},
{
"name": "Over100",
"value": "true",
"paramName": "",
"paramValue": ""
},
];
I would like to know all the possible ways, how to do that.
Thank You!
You can use push():
if(over100 != ''){
var addfilter = {"name": "Over100",
"value": "true",
"paramName": "",
"paramValue": ""}
filterarray.push(addfilter)
}
if(over100 != null) {
filterarray.push({"name": "Over100", "value": "true", "paramName": "", "paramValue": ""})
}
Related
How can I concatenate this json to obtain it:
complements = ["XYZ 3, CDE TR, AAA 5", "", "NDP 3, DDD FR"] ?
Each address can contain a set of complements which must be concatenated and separated by a comma.
P.s: I'm using javascript.
P.s2: Complements can be null like in the second group in JSON.
[
{
"postalcode": "1234",
"street": "ABC",
"number": "1",
"complement": [
{
"type": "B",
"name": "XYZ",
"description": "3"
},
{
"type": "C",
"name": "CDE",
"description": "TR"
},
{
"type": "D",
"name": "AAA",
"description": "5"
}
]
},
{
"postalcode": "444",
"street": "No complements",
"number": "5"
},
{
"postalcode": "2222",
"street": "BBB",
"number": "2",
"complement": [
{
"type": "E",
"name": "NDP",
"description": "3"
},
{
"type": "F",
"name": "DDD",
"description": "FR"
}
]
}
];
My code I'm getting this.complementsList.forEach is not a function.
getComplement(addressesResponse){
this.complementsList = JSON.parse(addressesResponse);
this.complementsList.forEach((item) => {
Object.defineProperty(item, 'complements', {
get: function() {
return this.complement.map((c) => `${c.name} ${c.description}`).join(', '); }
})
});
Source: https://salesforce.stackexchange.com/questions/367713/how-to-render-a-json-in-the-same-line-lwc
how i solved it :
arr.map((x)=>x.complement != null? (x.complement.map((y)=>y.name+' '+y.description)+"") :'');
Having a javascript object, you can go through the keys of the object and combine some of them into strings
It will look something like this:
const jsonObject = [{...}, {...}, ...]
const complements = [];
jsonObject.forEach((item) => {
let complement = item['complement'].reduce((result, currObj)
=> result += (currObj.name+' '+currObj.description), "");
complements.push(complement);
});
This is just an example. There are many ways to do it.
[
{
"PRODUCT_CATEGORY_ID": "1",
"NAME": "OTOMOBİL PARÇALARI",
"DESCRIPTION": "Araba Parçaları",
"ACTIVE": "True",
"PARENT_PRODUCT_CATEGORY_ID": "",
"children": [
{
"PRODUCT_CATEGORY_ID": "3",
"NAME": "HONDA PARÇALARI",
"DESCRIPTION": "Honda Parçaları",
"ACTIVE": "True",
"PARENT_PRODUCT_CATEGORY_ID": "1"
},
{
"PRODUCT_CATEGORY_ID": "4",
"NAME": "RENAULT PARÇALARI",
"DESCRIPTION": "Renault Parçaları",
"ACTIVE": "True",
"PARENT_PRODUCT_CATEGORY_ID": "1",
"children": [
{
"PRODUCT_CATEGORY_ID": "5",
"NAME": "MINIMAL RENAULT PARÇALARI",
"DESCRIPTION": "",
"ACTIVE": "True",
"PARENT_PRODUCT_CATEGORY_ID": "4"
}
]
}
]
}
]
I have a json with nested children and I want the paths of this object's children. nested children can be even more.I want as output is a string array.
[
"OTOMOBİL PARÇALARI"
"OTOMOBİL PARÇALARI > HONDA PARÇALARI"
"OTOMOBİL PARÇALARI > RENAULT PARÇALARI"
"OTOMOBİL PARÇALARI > RENAULT PARÇALARI > MINIMAL RENAULT PARÇALARI"
]
I tried
function findAllChildren (id, results, depth) {
for (d in data) {
if (data[d].parent == id) {
data[d].depth = depth
results.push(data[d])
findAllChildren(data[d].id, results, depth + 1)
}
}
}
var results = []
findAllChildren(1, results, 0)
return results.map(function (element) { return Array(element.depth + 1).join(' -> ') + element.name})
Thank you for your help in advance, I searched and couldn't find it, you can share it with me in the link.
You can use a a recursive approach, say, creating a function getPaths that will get a list of paths using a property key as an input.
In this case we'll use 'NAME', and build up an array of the relevant paths.
let input = [ { "PRODUCT_CATEGORY_ID": "1", "NAME": "OTOMOBİL PARÇALARI", "DESCRIPTION": "Araba Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "", "children": [ { "PRODUCT_CATEGORY_ID": "3", "NAME": "HONDA PARÇALARI", "DESCRIPTION": "Honda Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "1" }, { "PRODUCT_CATEGORY_ID": "4", "NAME": "RENAULT PARÇALARI", "DESCRIPTION": "Renault Parçaları", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "1", "children": [ { "PRODUCT_CATEGORY_ID": "5", "NAME": "MINIMAL RENAULT PARÇALARI", "DESCRIPTION": "", "ACTIVE": "True", "PARENT_PRODUCT_CATEGORY_ID": "4" } ] } ] } ]
function getPaths(obj, property, path = '') {
const paths = [];
for(let k in obj) {
if (typeof(obj[k]) === 'object') {
paths.push(...getPaths(obj[k], property, path));
} else if (k === property) {
path += (path ? ' > ' : '') + obj[k];
paths.push(path)
}
}
return paths;
}
console.log('Paths:', getPaths(input, 'NAME'))
In the ui, I have created a custom transaction search with vendor lines. How do I access vendor lines values? I have something like this:
var veResults = vendorSearch.run().getRange({
start: 0,
end: 50
});
for(var i = 0; i < veResults.length; i++)
{
var vendorName = veResults[i].getValue({name: 'vendorLine.entityid');
context.response.write(vendorName);
};
If I print veResults, I get the following json:
[
{
"recordType": "vendorpayment",
"id": "210",
"values": {
"employee.entityid": "",
"trandate": "5/20/2015",
"print": "Print",
"type": [
{
"value": "VendPymt",
"text": "Bill Payment"
}
],
"payrollbatch": "",
"tranid": "2009",
"entity": [
{
"value": "35",
"text": "Pacific Bell Telephone"
}
],
"account": [
{
"value": "1",
"text": "1000 Checking"
}
],
"otherrefnum": "",
"statusref": [],
"trackingnumbers": "",
"memo": "",
"currency": [
{
"value": "1",
"text": "USA"
}
],
"expectedreceiptdate": "",
"trandate_1": "5/20/2015",
"enddate": "",
"item": [],
"vendorLine.entityid": "Pacific Bell Telephone",
"vendorLine.billaddress": "Pacific Bell Telephone\nPacific Bell Payment Center\nSacramento CA 95887-0001\nUS"
}
...]
So, the value is there, how do I get it? The search object has only getValue and getText methods, none of which work in this case.
There is a syntax error with this line
var vendorName = veResults[i].getValue({'vendorLine.entityid');
It should be
var vendorName = veResults[i].getValue('vendorLine.entityid');
Kindly help me in sorting the below JSON list in the controller and display in the view.
Actually orderBy filter sorting one level, But I need it sort even for childs recursively.
Input:
R2
-->S4
------>T5
------>T4
-->S3
R1
-->S2
------>T2
------>T1
-->S1
Output:
R1
-->S1
------>T1
------>T2
-->S2
R2
-->S3
------>T4
------>T5
-->S4
Please find the sample in Plunker.
http://plnkr.co/edit/JslHwJ1CBREKf6FgYHaZ?p=preview
var sortNames = function(arr){
arr = arr.sort(function(a,b){
return a.name > b.name;
})
for (var i = 0; i < arr.length; i++){
if (arr[i].childs){
sortNames(arr[i].childs)
}
}
return arr;
}
var names = [
{
"name": "Root1",
"Id": "2f3d17cb-d9e2-4e99-882d-546767f2765d",
"status": "",
"dispName": "",
"imageURL": "",
"childCount": "",
"childs": [
{
"name": "Sub1",
"Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f98f",
"childs": [
{
"name": "Template1",
"Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f981",
"status": "",
"dispName": "",
"imageURL": ""
},
{
"name": "Template2",
"Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f982",
"status": "",
"dispName": "",
"imageURL": ""
}
]
},
{
"name": "Template3",
"Id": "ff8b3896-3b80-4e1b-be89-52a82ec9f981"
}
]
},
{
"name": "Root2",
"Id": "ea0586e7-02cf-4359-94ba-8d9623590dfe",
"childs": [
{
"name": "Sub2",
"Id": "6f1b3a60-d295-413e-92ef-1c713446e6c9",
"childs": [
{
"name": "Template4",
"Id": "6f1b3a60-d295-413e-92ef-1c713446e6c1"
},
{
"name": "Template5",
"Id": "6f1b3a60-d295-413e-92ef-1c713446e6c2"
}
]
}
]
}
];
sortNames(names);
I've been looking for a solution for this with no luck.
I have an JSON object:
arr = [
{
"id": "1",
"dataId" : "",
"rel": ""
},
{
"id": "2",
"dataId" : "",
"rel": ""
},
{
"id": "3",
"dataId" : "",
"rel": ""
},
{
"id": "4",
"dataId" : "1",
"rel": ""
}];
I need a way to manipulate this array with javascript (jquery is allowed), so when "arr[a].dataId" match with the "arr[b].id", take "arr[a]" element and insert into "arr[b]" creating a new array like this:
newArr = [
{
"id": "1",
"dataId" : "",
"rel" : [
{
"id": "4",
"dataId" : "1"
}
]
},
{
"id": "2",
"dataId" : "",
"rel": ""
},
{
"id": "3",
"dataId" : "",
"rel": ""
}
];
I hope you understand what I'm asking, any help will be appreciated.
Thanks in advance
This one does move the elements and doesn't manipulate the array while it's iterating over it. Also doesn't need to extend the Array.prototype. One downside is that you're going to get holes in your array (indexes of elements won't be 0 to length-1), but from what I can see it's not important in your use case.
var arr = [
{
"id": "1",
"dataId" : "",
"rel": ""
},
{
"id": "2",
"dataId" : "3",
"rel": ""
},
{
"id": "3",
"dataId" : "",
"rel": ""
},
{
"id": "4",
"dataId" : "1",
"rel": ""
}];
var indexesToMoveMap = {};
$.each(arr, function(outerIndex, outerElem) {
if (outerElem.dataId) {
$.each(arr, function(innerIndex, innerElem) {
if (innerElem.id === outerElem.dataId) {
indexesToMoveMap[outerIndex] = innerIndex;
}
});
}
});
$.each(indexesToMoveMap, function(indexToMove, indexToMoveTo) {
arr[indexToMoveTo].rel = arr[indexToMoveTo].rel || [];
arr[indexToMoveTo].rel.push(arr[indexToMove]);
delete arr[indexToMove];
});
console.log(arr);
Try this:
var arr = [
{
"id": "1",
"dataId" : "",
"rel": ""
},
{
"id": "2",
"dataId" : "",
"rel": ""
},
{
"id": "3",
"dataId" : "",
"rel": ""
},
{
"id": "4",
"dataId" : "1",
"rel": ""
}];
$(arr).each(function(index,value){
if(value.dataId)
{
$(arr).each(function(ind,val){
if(val.id==value.dataId)
{
if(val.rel)
{
val.rel.push({"id":value.id,"dataId":value.dataId});
}
else
{
val.rel=[{"id":value.id,"dataId":value.dataId}];
}
}
});
}
});
console.log(arr);
Fiddle http://jsfiddle.net/7g23B/
Something like this?
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
var arr = [
{
"id": "1",
"dataId" : "",
"rel": []
},
{
"id": "2",
"dataId" : "",
"rel": []
},
{
"id": "3",
"dataId" : "",
"rel": []
},
{
"id": "4",
"dataId" : "1",
"rel": []
}];
var index = 0;
for ( var m = 0; m < arr.length; m++ ) {
for ( var i = 0; i < arr.length; i++ ) {
if ( arr[index].id === arr[i].dataId ) {
arr[index].rel.push (arr[i]);
arr.remove(i);
}
}
index++;
}
console.log(JSON.stringify(arr, null, 4))