Store user input in an array or object [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I would like to know whether it is easier to store all the data from a text input where the user has to enter their surname in an array or in an object. And how do I do so?.
Thanks in advance

You could do something like this:
var dataArray = [];
function storeData() {
var data = document.getElementById("yourInput").value;
dataArray.push(data);
}
This method uses arrays - personally I find them more easier to use than objects.

Related

How to Filter an array of strings based on first word in the string in javascript? [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 1 year ago.
Improve this question
With the following array of strings in javascript, is there a way to use the .filter to filter by the first word?
['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two']
For example to .filter(x = 'Desk_*')
['Desk_One', 'Desk_Two', 'Desk_Three']
Maybe this needs to be a regex
You can use startsWith() function like this:
let array = ['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two'];
let result = array.filter((item)=>item.startsWith(array[0].split('_')[0]));
console.log(result)

How to get every element in an array in an if statement js [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’m trying to check names through an array and I don’t want to have to use
if (<array name>[0,1,2,3,4,5,6,7])
{ code in here }
depend on what you are trying to do u can use forEach or map
let arr = [1,2,3,4,5,6,7,8,9,10];
arr.forEach((item)=>{
console.log(`${item} is greater than 5 : ${item>5}`)
})

what is the best way to write this: [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
let valor = value.replace("'","")
valor=valor.replace('"',"")
//or
name=value.replace("'","").replace('"',"")
A better option might be to use a single .replace instead, and use a regular expression to match either ' or ":
const valor = value.replace(/['"]/g, '');

How do I get length of my Json Object [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 7 years ago.
Improve this question
Below is my JSON object
{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}
How do I get length of this.In this example it should return me 3.
My Object name is DecisionObj which have above listed elements.
var string = '{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}';
var object = JSON.parse(string);
alert(object.Decision.length);
Arrays have length property. You can use that.
var length = DecisionObj.length;

Key-value to object [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 8 years ago.
Improve this question
I got a key-value object like this:
items = {1:"a",2:"v",3:"u"};
But i want it like this.
items = [{"key":"1","value","a"},{"key":"2","value","v"},{"key":"3","value","u"}];
What is the best way to do this?
I have already tried to do this with $.each.
var items = {1:"a",2:"v",3:"u"};
var newItems = [];
$(items).each(function(k,v){
newItems.push({"key":k,"value":v});
});
And please tell me what is wrong with this question!
Try this-
var items = {1:"a",2:"v",3:"u"};
var obj=[];
for(var i in items){
obj.push({"key":i,"value":items[i]});
}
console.log(obj)

Categories