Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to populate a nested array.
The first array option_array is created like this
var option_array =[];
if(!option_array.includes(row.option.option_id))
{
option_array .push({
option_id:row.option.option_id,
option_name:row.option.option_name,
max:row.option.max,
min:row.option.min,
value:row.option.value,
price:row.option.price
})
}
And now, I am creating another array option_group
var option_group=[];
if(!option_group.includes(row.option_group_id))
{
option_group.push({
option_group_id:row.option_group_id,
option_group_name:row.option_group_name,
max:row.max,
min:row.min,
option:option_array
})
}
And I want to modify option_group whereas it will add only option:option_array where current row.option_group_id is equal to the option_group.option_group.option_group_id
.includes checks if the array includes a certain item. Since you have an array of objects, you can't check if the array includes row.option_group_id. You can use find instead. Get the object with row.option_group_id from the array. If it exists, update it. Else, add a new object to the array
const found = option_group.find(a => a.option_group_id == row.option_group_id)
if (found) {
found.option = row.option_array; // update the found object
} else {
option_group.push({
option_group_id: row.option_group_id,
option_group_name: row.option_group_name,
....
})
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So I have a JSON object and I just want to append the to the next row the data in it so that the value goes to whatever column matches the key.
function loadData() {
let obj=JSON.parse(your json string);
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');//sheet name
const hdrrow=1;//header row
const hA=sh.getRange(hdrrow,1,1,sh.getLastColumn()).getValues()[0];//header values at least some of which should match keys
let row=new Array(hA.length);//make array same length as header row
hA.forEach((h,i)=>{
if(obj.hasOwnProperty(h)) {
row[i]=obj[h];
}
});
sh.appendRow(row);
}
I assume string is in the form
'{"COL1":6,"COL2":12,"COL3":4,"COL4":21,"COL5":6,"COL6":19,"COL7":23,"COL8":20,"COL9":2,"COL10":18}'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a scenario where I have an INPUT, The User enters some text, like for ex: Cat.
I need to search an Array and check if the input from the User is present in my Array.
If it is present I ould want to remove that item from the Array and return a new Array with that item removed.
Suppose I have an arraylist like,
let arrayList = ["cat","mouse","dog"];
User INPUT = cat;
I want to check if the cat is present in arrayList , If yes then I want to remove the cat from the arrayList and want to have a new array that does not contain cat in it. Hence any INPUT provided by User needs to be removed from an array and return me a new array without that item.
You can use filter function to filter the array. Reference link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
function filterArray(arr, searchString) {
var result = arr.filter(function(e) {
return e != searchString;
});
return result;
}
filterArray(arrayList, "cat") // This will return ["mouse", "dog"];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to Javascript and jQuery. I am trying to create something like this (object) below and read the keys and retrieve the values.
var members = {unit:1,name: ["AA", "AB"],userid:["0001","0002"],
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]};
if 'Unit' == 1 then display name array.
Basically I want loop through the object and retrieve the value.
I think you member should be like this
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
members.forEach(function(v,i){
if(v.unit==1)
{
console.log(v.name);
}
});
I guess this is want you wanted, an object within an object. Where 'unit' is the key for retrieving the objects within the object.
var members = {
1: {
name: ["test","test"],
userid: ["0001","0002"]},
2: {
name: ["test2", "test2"],
userid: ["0011", "0012"]}
};
console.log(members[1]);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this array [ABC, QWE, XYZ]
I would like to turn it into ['ABC', 'QWE', 'XYZ']
When I try to manipulate values in the current array I get: ReferenceError: ABC is not defined
Any ideas on how should I do it?
Thanks!
Convert arrays element types:
Number to strings
var strArr = [1,2,3,4,5].map(String);
// Result: ["1","2","3","4","5"]
We can't do that directly but after little bit change you can do that...
So the current array you said like array [ABC, QWE, XYZ],
Lets design you keys in object first:
var obj = {
ABC:1, QWE:'somevalue', XYZ:new Date()
}
So I created object obj having your variables lets say the three variables, now lets convert:
var arr = [];
for (var key in obj){
console.log(key, obj[key]);
arr.push(String(key));
}
console.log(arr);// you will see the desire result.
Running example here : example