create javascript object from for loop
up vote
-3
down vote
favorite
I want to create a sortable javascript object that looks like this:
myArray = [
name: "Jones, James", ef: "35", mem: "2018.12.10",
name: "Smith, Paul", ef: "35", mem: "2018.09.12",
name: "Washington, George", ef: "35", mem: "2018.08.16"
];
I also plan to sort myArray() by name.
myArray.sort(function(a, b)
return a.name - b.name;
);
THE JAVASCRIPT:
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
I sorted the myArray:
0: name: "Abercrombie, Bill", rank: "1537", uscf: "9999", ef: "3", exp: "2019.01.12", …
1: name: "Baken, Clif", rank: "1802", uscf: "9999", ef: "3", exp: "2019.09.18", …
2: name: "Kukla, Ollie", rank: "2014", uscf: "0920", ef: "", exp: "2019.08.12", …
3: name: "Lincoln, Abraham", rank: "2023", uscf: "0119", ef: "", exp: "2019.09.06", …
4: name: "Washington, George", rank: "1563", uscf: "9999", ef: "3", exp: "2019.01.16", …
How do I iterate over the sorted myArray() to populate the form? I think I need something like this:
console.log(myArray.length);
for (i = 1; i <= myArray.length; i++)
// where id "P1" is the name input field in the form which we are populating
document.getElementById("P" +i).value = myArray[name];
javascript jquery
|
show 18 more comments
up vote
-3
down vote
favorite
I want to create a sortable javascript object that looks like this:
myArray = [
name: "Jones, James", ef: "35", mem: "2018.12.10",
name: "Smith, Paul", ef: "35", mem: "2018.09.12",
name: "Washington, George", ef: "35", mem: "2018.08.16"
];
I also plan to sort myArray() by name.
myArray.sort(function(a, b)
return a.name - b.name;
);
THE JAVASCRIPT:
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
I sorted the myArray:
0: name: "Abercrombie, Bill", rank: "1537", uscf: "9999", ef: "3", exp: "2019.01.12", …
1: name: "Baken, Clif", rank: "1802", uscf: "9999", ef: "3", exp: "2019.09.18", …
2: name: "Kukla, Ollie", rank: "2014", uscf: "0920", ef: "", exp: "2019.08.12", …
3: name: "Lincoln, Abraham", rank: "2023", uscf: "0119", ef: "", exp: "2019.09.06", …
4: name: "Washington, George", rank: "1563", uscf: "9999", ef: "3", exp: "2019.01.16", …
How do I iterate over the sorted myArray() to populate the form? I think I need something like this:
console.log(myArray.length);
for (i = 1; i <= myArray.length; i++)
// where id "P1" is the name input field in the form which we are populating
document.getElementById("P" +i).value = myArray[name];
javascript jquery
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
1
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your<input>
elements or theirid
attributes. It's very confusing because your IDs areP4
,E4
andX4
which really doesn't make sense given "P1-P48"
– Phil
Nov 9 at 2:34
|
show 18 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I want to create a sortable javascript object that looks like this:
myArray = [
name: "Jones, James", ef: "35", mem: "2018.12.10",
name: "Smith, Paul", ef: "35", mem: "2018.09.12",
name: "Washington, George", ef: "35", mem: "2018.08.16"
];
I also plan to sort myArray() by name.
myArray.sort(function(a, b)
return a.name - b.name;
);
THE JAVASCRIPT:
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
I sorted the myArray:
0: name: "Abercrombie, Bill", rank: "1537", uscf: "9999", ef: "3", exp: "2019.01.12", …
1: name: "Baken, Clif", rank: "1802", uscf: "9999", ef: "3", exp: "2019.09.18", …
2: name: "Kukla, Ollie", rank: "2014", uscf: "0920", ef: "", exp: "2019.08.12", …
3: name: "Lincoln, Abraham", rank: "2023", uscf: "0119", ef: "", exp: "2019.09.06", …
4: name: "Washington, George", rank: "1563", uscf: "9999", ef: "3", exp: "2019.01.16", …
How do I iterate over the sorted myArray() to populate the form? I think I need something like this:
console.log(myArray.length);
for (i = 1; i <= myArray.length; i++)
// where id "P1" is the name input field in the form which we are populating
document.getElementById("P" +i).value = myArray[name];
javascript jquery
I want to create a sortable javascript object that looks like this:
myArray = [
name: "Jones, James", ef: "35", mem: "2018.12.10",
name: "Smith, Paul", ef: "35", mem: "2018.09.12",
name: "Washington, George", ef: "35", mem: "2018.08.16"
];
I also plan to sort myArray() by name.
myArray.sort(function(a, b)
return a.name - b.name;
);
THE JAVASCRIPT:
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
I sorted the myArray:
0: name: "Abercrombie, Bill", rank: "1537", uscf: "9999", ef: "3", exp: "2019.01.12", …
1: name: "Baken, Clif", rank: "1802", uscf: "9999", ef: "3", exp: "2019.09.18", …
2: name: "Kukla, Ollie", rank: "2014", uscf: "0920", ef: "", exp: "2019.08.12", …
3: name: "Lincoln, Abraham", rank: "2023", uscf: "0119", ef: "", exp: "2019.09.06", …
4: name: "Washington, George", rank: "1563", uscf: "9999", ef: "3", exp: "2019.01.16", …
How do I iterate over the sorted myArray() to populate the form? I think I need something like this:
console.log(myArray.length);
for (i = 1; i <= myArray.length; i++)
// where id "P1" is the name input field in the form which we are populating
document.getElementById("P" +i).value = myArray[name];
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
function store()
var myArray = ;
var i;
for (i = 0; i < 10; i++)
NAME = document.getElementById('P' + i).value;
EF = document.getElementById('E' + i).value;
MEM = document.getElementById('M' + i).value;
// This code doesn't seem to add records to myArray()
myArray.push(
name: 'NAME',
ef: 'EF',
mem: 'MEM'
);
console.log(myArray.length);
store();
javascript jquery
javascript jquery
edited Nov 12 at 8:01
asked Nov 8 at 23:59
verlager
29631328
29631328
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
1
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your<input>
elements or theirid
attributes. It's very confusing because your IDs areP4
,E4
andX4
which really doesn't make sense given "P1-P48"
– Phil
Nov 9 at 2:34
|
show 18 more comments
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
1
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your<input>
elements or theirid
attributes. It's very confusing because your IDs areP4
,E4
andX4
which really doesn't make sense given "P1-P48"
– Phil
Nov 9 at 2:34
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
1
1
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your
<input>
elements or their id
attributes. It's very confusing because your IDs are P4
, E4
and X4
which really doesn't make sense given "P1-P48"– Phil
Nov 9 at 2:34
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your
<input>
elements or their id
attributes. It's very confusing because your IDs are P4
, E4
and X4
which really doesn't make sense given "P1-P48"– Phil
Nov 9 at 2:34
|
show 18 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Since you use use jQuery, why not build myArray
like this:
for (n = 1; n <= 3; n++)
myArray.push(
name: $('#P' + n).val(),
ef: $('#E' + n).val(),
mem: $('#M' + n).val()
);
For sorting the myArray
: (credits to the MDN reference as well as this answer)
myArray.sort((a, b) =>
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
);
Then for putting back the myArray
values into the corresponding input
fields:
myArray.forEach((item, i) =>
let n = i + 1;
$('#P' + n).val(item.name);
$('#E' + n).val(item.ef);
$('#M' + n).val(item.mem);
// ... do the same for other fields
);
add a comment |
up vote
1
down vote
There are a few issues with the code:
you are not declaring the variables
NAME
,EF
,MEM
.This means these are implicit globals (!) and your code might have unintended side-effects.
Solution: use
var
,let
or even better,const
to declare these variables in the block scope.You are pushing strings, not variables.
when you do
myArray.push(name: 'NAME')
you are saying put the string value'NAME'
as the value forname
.Solution: remove the quotes, use
name: NAME
, or even better, change the variable toname
and use the ES2015 shorthand notation:myArray.push(name, ef, mem);
prefer to use
const
if you are not reassigning variables. Uselet
in all other cases.This simply makes your code more robust as they have fewer quirks than using the old
var
.
Example:
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
add a comment |
up vote
0
down vote
Two Steps:
1- Make Array
2- Sort its items using sort function
remember that localStorage sets and get strings so parse them using JSON.parse() method
Sort Function: (this sorts alphabetically with property name)
Array.prototype.sort()
Demo:
let result = array.sort((a.name, b.name) => a.name - b.name)
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217929%2fcreate-javascript-object-from-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Since you use use jQuery, why not build myArray
like this:
for (n = 1; n <= 3; n++)
myArray.push(
name: $('#P' + n).val(),
ef: $('#E' + n).val(),
mem: $('#M' + n).val()
);
For sorting the myArray
: (credits to the MDN reference as well as this answer)
myArray.sort((a, b) =>
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
);
Then for putting back the myArray
values into the corresponding input
fields:
myArray.forEach((item, i) =>
let n = i + 1;
$('#P' + n).val(item.name);
$('#E' + n).val(item.ef);
$('#M' + n).val(item.mem);
// ... do the same for other fields
);
add a comment |
up vote
1
down vote
accepted
Since you use use jQuery, why not build myArray
like this:
for (n = 1; n <= 3; n++)
myArray.push(
name: $('#P' + n).val(),
ef: $('#E' + n).val(),
mem: $('#M' + n).val()
);
For sorting the myArray
: (credits to the MDN reference as well as this answer)
myArray.sort((a, b) =>
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
);
Then for putting back the myArray
values into the corresponding input
fields:
myArray.forEach((item, i) =>
let n = i + 1;
$('#P' + n).val(item.name);
$('#E' + n).val(item.ef);
$('#M' + n).val(item.mem);
// ... do the same for other fields
);
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Since you use use jQuery, why not build myArray
like this:
for (n = 1; n <= 3; n++)
myArray.push(
name: $('#P' + n).val(),
ef: $('#E' + n).val(),
mem: $('#M' + n).val()
);
For sorting the myArray
: (credits to the MDN reference as well as this answer)
myArray.sort((a, b) =>
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
);
Then for putting back the myArray
values into the corresponding input
fields:
myArray.forEach((item, i) =>
let n = i + 1;
$('#P' + n).val(item.name);
$('#E' + n).val(item.ef);
$('#M' + n).val(item.mem);
// ... do the same for other fields
);
Since you use use jQuery, why not build myArray
like this:
for (n = 1; n <= 3; n++)
myArray.push(
name: $('#P' + n).val(),
ef: $('#E' + n).val(),
mem: $('#M' + n).val()
);
For sorting the myArray
: (credits to the MDN reference as well as this answer)
myArray.sort((a, b) =>
let nameA = a.name.toUpperCase();
let nameB = b.name.toUpperCase();
return nameA < nameB ? -1 : nameA > nameB ? 1 : 0;
);
Then for putting back the myArray
values into the corresponding input
fields:
myArray.forEach((item, i) =>
let n = i + 1;
$('#P' + n).val(item.name);
$('#E' + n).val(item.ef);
$('#M' + n).val(item.mem);
// ... do the same for other fields
);
answered Nov 12 at 8:32
Sally CJ
7,5722416
7,5722416
add a comment |
add a comment |
up vote
1
down vote
There are a few issues with the code:
you are not declaring the variables
NAME
,EF
,MEM
.This means these are implicit globals (!) and your code might have unintended side-effects.
Solution: use
var
,let
or even better,const
to declare these variables in the block scope.You are pushing strings, not variables.
when you do
myArray.push(name: 'NAME')
you are saying put the string value'NAME'
as the value forname
.Solution: remove the quotes, use
name: NAME
, or even better, change the variable toname
and use the ES2015 shorthand notation:myArray.push(name, ef, mem);
prefer to use
const
if you are not reassigning variables. Uselet
in all other cases.This simply makes your code more robust as they have fewer quirks than using the old
var
.
Example:
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
add a comment |
up vote
1
down vote
There are a few issues with the code:
you are not declaring the variables
NAME
,EF
,MEM
.This means these are implicit globals (!) and your code might have unintended side-effects.
Solution: use
var
,let
or even better,const
to declare these variables in the block scope.You are pushing strings, not variables.
when you do
myArray.push(name: 'NAME')
you are saying put the string value'NAME'
as the value forname
.Solution: remove the quotes, use
name: NAME
, or even better, change the variable toname
and use the ES2015 shorthand notation:myArray.push(name, ef, mem);
prefer to use
const
if you are not reassigning variables. Uselet
in all other cases.This simply makes your code more robust as they have fewer quirks than using the old
var
.
Example:
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
add a comment |
up vote
1
down vote
up vote
1
down vote
There are a few issues with the code:
you are not declaring the variables
NAME
,EF
,MEM
.This means these are implicit globals (!) and your code might have unintended side-effects.
Solution: use
var
,let
or even better,const
to declare these variables in the block scope.You are pushing strings, not variables.
when you do
myArray.push(name: 'NAME')
you are saying put the string value'NAME'
as the value forname
.Solution: remove the quotes, use
name: NAME
, or even better, change the variable toname
and use the ES2015 shorthand notation:myArray.push(name, ef, mem);
prefer to use
const
if you are not reassigning variables. Uselet
in all other cases.This simply makes your code more robust as they have fewer quirks than using the old
var
.
Example:
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
There are a few issues with the code:
you are not declaring the variables
NAME
,EF
,MEM
.This means these are implicit globals (!) and your code might have unintended side-effects.
Solution: use
var
,let
or even better,const
to declare these variables in the block scope.You are pushing strings, not variables.
when you do
myArray.push(name: 'NAME')
you are saying put the string value'NAME'
as the value forname
.Solution: remove the quotes, use
name: NAME
, or even better, change the variable toname
and use the ES2015 shorthand notation:myArray.push(name, ef, mem);
prefer to use
const
if you are not reassigning variables. Uselet
in all other cases.This simply makes your code more robust as they have fewer quirks than using the old
var
.
Example:
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
function store()
const myArray = ;
for (let i = 0; i < 10; i++)
// Replaced getElementByID since it's not relevant
const name = 'some name';
const ef = 'some string';
const mem = 'some value';
// This code doesn't seem to add records to myArray()
myArray.push(name, ef, mem);
console.log(myArray.length);
store();
answered Nov 12 at 0:59
lucascaro
3,39611530
3,39611530
add a comment |
add a comment |
up vote
0
down vote
Two Steps:
1- Make Array
2- Sort its items using sort function
remember that localStorage sets and get strings so parse them using JSON.parse() method
Sort Function: (this sorts alphabetically with property name)
Array.prototype.sort()
Demo:
let result = array.sort((a.name, b.name) => a.name - b.name)
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
|
show 3 more comments
up vote
0
down vote
Two Steps:
1- Make Array
2- Sort its items using sort function
remember that localStorage sets and get strings so parse them using JSON.parse() method
Sort Function: (this sorts alphabetically with property name)
Array.prototype.sort()
Demo:
let result = array.sort((a.name, b.name) => a.name - b.name)
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
Two Steps:
1- Make Array
2- Sort its items using sort function
remember that localStorage sets and get strings so parse them using JSON.parse() method
Sort Function: (this sorts alphabetically with property name)
Array.prototype.sort()
Demo:
let result = array.sort((a.name, b.name) => a.name - b.name)
Two Steps:
1- Make Array
2- Sort its items using sort function
remember that localStorage sets and get strings so parse them using JSON.parse() method
Sort Function: (this sorts alphabetically with property name)
Array.prototype.sort()
Demo:
let result = array.sort((a.name, b.name) => a.name - b.name)
edited Nov 12 at 0:24
answered Nov 10 at 1:58
Microsmsm
1,8851624
1,8851624
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
|
show 3 more comments
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
How can this work...? I have localStorage with SP1 to SP48 as sort items. I don't see any reference to this in the expression you have kindly supplied.
– verlager
Nov 10 at 3:44
You are right. I get it.
– verlager
Nov 11 at 7:26
You are right. I get it.
– verlager
Nov 11 at 7:26
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
First we have the populate a sortable javascript object and then fill in the form from the sorted list.
– verlager
Nov 11 at 22:04
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
Yes... I updated the answer.. you can mark it as solution if it helps
– Microsmsm
Nov 12 at 0:19
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
A simple array is not useful in this case. Please see the orginal question. I have to sort the "array" and then iterate over the "array" to extract vars and populate the form.
– verlager
Nov 12 at 0:37
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217929%2fcreate-javascript-object-from-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what have you tried so far?kindly include that in OP
– guradio
Nov 9 at 0:02
I am new to localStorage and I don't know what I'm doing. I need assistance. I can do some things but this pretty advanced. I'm really in over my head, guradio.
– verlager
Nov 9 at 0:10
1
You can store an array or single object in one localStorage key by using JSON.stringify() to set and JSON.parse() in the get. Then loop over that array/object and set values within the loop with very little code needed
– charlietfl
Nov 9 at 0:22
I think: localStorage.setItem('SP4', JSON.stringify(document.getElementById("P4").value))); (is correct). But how do I bring the local storage into a sortable array with JSON.stringify ?
– verlager
Nov 9 at 0:43
What exactly is it you want to sort by? You mention "P1-P48" but what does that relate to? Is it the values in your
<input>
elements or theirid
attributes. It's very confusing because your IDs areP4
,E4
andX4
which really doesn't make sense given "P1-P48"– Phil
Nov 9 at 2:34