JavaScript Array Search and Remove [closed] - javascript

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"];

Related

How do you append JSON object data to the next available row so that each key value goes to the corresponding column that matches? [closed]

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}'

JavaScript parse text containing name value pairs [closed]

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 some text string which is like a JSON string, but not an exact one. It is like below
First Name: John
Last name : Doe
Address :London
It will be better if an object can be extracted from this string, so that I can iterate over its properties (First Name, Address etc.) with for...in loop.
Any idea about how this can be done? Thanks in advance!
Simply make use of String.prototype.split() (once, to extract each line into separate item, then once again, to separate property names from values):
const str = `First Name: John
Last name : Doe
Address :London`,
result = str
.split("\n")
.reduce((acc, substr) => {
const [prop, value] = substr.split(/\s*:\s*/)
acc[prop] = value
return acc
}, {})
console.log(result)
.as-console-wrapper{min-height:100%;}

Javascript Convert 'List()' to list [closed]

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 2 years ago.
Improve this question
I received data from another API. But I don't know how to convert and use data.
data example
{
...
answer:'List(value1,value2,value3,value4)',
...
}
I want to iterate all answer nodes. please help.
Approach
You could capture the group between List(...) and split that by comma ,
Regex test: https://regex101.com/r/xFf39R/1
const answer = 'List(value1,value2,value3,value4)'
const res = /List\((.*)\)/.exec(answer)[1].split(',')
console.log(res)
Reference
RegExp.prototype.exec()
Return value
[...] The returned array has the matched text as the first item, and then one item for each parenthetical capture group of the matched text.
Reading your data example... It seems like the answer is carrying a String as the single quote is presented in your data example.
answer:'List(value1,value2,value3,value4)'
^ ^
And the List(value1,value2,value3,value4) actually looks like some python List in system print.
Well, but this does not help in your direct question.
Assuming your want to get all four values into an array in javascript, do the followings
let data_example = {
...
'answer':'List(value1,value2,value3,value4)',
...
}
let answer_string = data_example['answer'].slice(5,-1)
let your_list = answer_string.split(',')
console.log(your_list)
//["value1","value2","value3","value4"]
But be careful... I assume your List() always start with 'List(' and end with ')'
See more on slice and split

Adding array into an certain array index [closed]

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,
....
})
}

Convert array values into strings in javascript [closed]

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

Categories