How find a value within an array, with a users input











up vote
2
down vote

favorite












var x1 ='&spades',
y1 ='&clubs'
z1 ='&hearts';
var x2 = ' ', y2 = ' ', z2 = ' ';
var x3 = ' ', y3 = ' ', z3 = ' ';


var Array = [x1,y1,z1,
x2,y2,z2,
x3,y3,z3];


Then I get an input from the user asking them to select a location (ex. x1) store it as input 1, then ask them for another location (ex. x2) and store that as input 2, and I have to swap the locations of the value.



I initially tried something like Array[input1] = Array[input2]



But Array[input1] is undefined even though input1 = x1 because Array[x1] is still undefined.



So how do I make Array[x1] = Array[0] so I can swap the values or is there an easier way?



Sorry if my question is poorly formed.










share|improve this question
























  • For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
    – Jim B.
    Nov 12 at 4:22












  • use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
    – Sanira
    Nov 12 at 4:25












  • @CertainPerformance Yes the input is a string.
    – David
    Nov 12 at 4:28










  • As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
    – Suyash Gulati
    Nov 12 at 4:30










  • @JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
    – David
    Nov 12 at 4:30















up vote
2
down vote

favorite












var x1 ='&spades',
y1 ='&clubs'
z1 ='&hearts';
var x2 = ' ', y2 = ' ', z2 = ' ';
var x3 = ' ', y3 = ' ', z3 = ' ';


var Array = [x1,y1,z1,
x2,y2,z2,
x3,y3,z3];


Then I get an input from the user asking them to select a location (ex. x1) store it as input 1, then ask them for another location (ex. x2) and store that as input 2, and I have to swap the locations of the value.



I initially tried something like Array[input1] = Array[input2]



But Array[input1] is undefined even though input1 = x1 because Array[x1] is still undefined.



So how do I make Array[x1] = Array[0] so I can swap the values or is there an easier way?



Sorry if my question is poorly formed.










share|improve this question
























  • For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
    – Jim B.
    Nov 12 at 4:22












  • use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
    – Sanira
    Nov 12 at 4:25












  • @CertainPerformance Yes the input is a string.
    – David
    Nov 12 at 4:28










  • As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
    – Suyash Gulati
    Nov 12 at 4:30










  • @JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
    – David
    Nov 12 at 4:30













up vote
2
down vote

favorite









up vote
2
down vote

favorite











var x1 ='&spades',
y1 ='&clubs'
z1 ='&hearts';
var x2 = ' ', y2 = ' ', z2 = ' ';
var x3 = ' ', y3 = ' ', z3 = ' ';


var Array = [x1,y1,z1,
x2,y2,z2,
x3,y3,z3];


Then I get an input from the user asking them to select a location (ex. x1) store it as input 1, then ask them for another location (ex. x2) and store that as input 2, and I have to swap the locations of the value.



I initially tried something like Array[input1] = Array[input2]



But Array[input1] is undefined even though input1 = x1 because Array[x1] is still undefined.



So how do I make Array[x1] = Array[0] so I can swap the values or is there an easier way?



Sorry if my question is poorly formed.










share|improve this question















var x1 ='&spades',
y1 ='&clubs'
z1 ='&hearts';
var x2 = ' ', y2 = ' ', z2 = ' ';
var x3 = ' ', y3 = ' ', z3 = ' ';


var Array = [x1,y1,z1,
x2,y2,z2,
x3,y3,z3];


Then I get an input from the user asking them to select a location (ex. x1) store it as input 1, then ask them for another location (ex. x2) and store that as input 2, and I have to swap the locations of the value.



I initially tried something like Array[input1] = Array[input2]



But Array[input1] is undefined even though input1 = x1 because Array[x1] is still undefined.



So how do I make Array[x1] = Array[0] so I can swap the values or is there an easier way?



Sorry if my question is poorly formed.







javascript html arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 5:46









Andreas

1,7861618




1,7861618










asked Nov 12 at 4:18









David

204




204












  • For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
    – Jim B.
    Nov 12 at 4:22












  • use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
    – Sanira
    Nov 12 at 4:25












  • @CertainPerformance Yes the input is a string.
    – David
    Nov 12 at 4:28










  • As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
    – Suyash Gulati
    Nov 12 at 4:30










  • @JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
    – David
    Nov 12 at 4:30


















  • For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
    – Jim B.
    Nov 12 at 4:22












  • use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
    – Sanira
    Nov 12 at 4:25












  • @CertainPerformance Yes the input is a string.
    – David
    Nov 12 at 4:28










  • As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
    – Suyash Gulati
    Nov 12 at 4:30










  • @JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
    – David
    Nov 12 at 4:30
















For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
– Jim B.
Nov 12 at 4:22






For starters, 'Array' is a keyword. Probably not a good idea to use it as a variable name. Next, the indices to your array are 0 through 9. JS has no idea how to map 'x2' to 4.
– Jim B.
Nov 12 at 4:22














use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
– Sanira
Nov 12 at 4:25






use a variable to temporarily store the value of Array[firstValueToSwap] value and store the Array[secondValueToSwap] in Array[firstValueToSwap] and store the value in temp to Array[secondValueToSwap]. It's the easiest way to swap two values. BTW your question is not that clear. hope this is what you're looking for
– Sanira
Nov 12 at 4:25














@CertainPerformance Yes the input is a string.
– David
Nov 12 at 4:28




@CertainPerformance Yes the input is a string.
– David
Nov 12 at 4:28












As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
– Suyash Gulati
Nov 12 at 4:30




As per my understanding, you have an array with 9 elements in it. Example: [ '&spades', '&clubs', '&hearts', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. Now you want to swap the values at 2 indexes (input1 and input2) for example if the user entered 0 and 2. The new array will be [ '&hearts', '&clubs', '&spades', ' ', ' ' , ' ' , ' ' , ' ' , ' ']. I am writing this comment so that community can understand the question correctly.
– Suyash Gulati
Nov 12 at 4:30












@JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
– David
Nov 12 at 4:30




@JimB. I know this isnt my real code my array is a lot bigger just a simple version, and I know that JS doesnt know how to map x2 to 4 but I was trying to see if there is any way otherwise i have to rewrite my program
– David
Nov 12 at 4:30












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.



Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:






const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:



([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);


If you find that confusing, an alternative is to actually use an intermediate variable:



const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;





share|improve this answer





















  • I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
    – David
    Nov 12 at 4:44










  • Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
    – David
    Nov 12 at 5:52


















up vote
0
down vote













Here's what your code might look like if you use an object:






    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);








share|improve this answer





















  • I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
    – David
    Nov 12 at 4:45










  • Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
    – David
    Nov 12 at 5:54











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255913%2fhow-find-a-value-within-an-array-with-a-users-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.



Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:






const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:



([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);


If you find that confusing, an alternative is to actually use an intermediate variable:



const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;





share|improve this answer





















  • I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
    – David
    Nov 12 at 4:44










  • Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
    – David
    Nov 12 at 5:52















up vote
2
down vote



accepted










Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.



Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:






const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:



([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);


If you find that confusing, an alternative is to actually use an intermediate variable:



const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;





share|improve this answer





















  • I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
    – David
    Nov 12 at 4:44










  • Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
    – David
    Nov 12 at 5:52













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.



Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:






const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:



([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);


If you find that confusing, an alternative is to actually use an intermediate variable:



const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;





share|improve this answer












Assuming the inputs are strings ('x1' and 'x2'), you need a way to represent the variable names in your data structure. One way would be to have, rather than an array of strings, an array of objects, containing a label and a value property. Then, just find the indicies of both labels for both inputs, and reassign those indicies.



Also, better not to assign to a variable named Array, because that will shadow the global Array object - name it something else instead, like arr:






const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





The destructuring line at the end there allows for swapping variables in two positions at once, without having to resort to an intermediate variable:



([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);


If you find that confusing, an alternative is to actually use an intermediate variable:



const orig1 = arr[index1];
arr[index1] = arr[index2];
arr[index2] = orig1;





const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);





const arr = [
{ label: 'x1', value: '&spades' },
{ label: 'y1', value: '&clubs' },
{ label: 'z1', value: '&hearts' }
// ...
];
// Inputs:
const label1 = 'x1';
const label2 = 'y1';

// Calculate indicies:
const [index1, index2] = [label1, label2].map(
findLabel => arr.findIndex(({ label }) => label === findLabel)
);
([arr[index1], arr[index2]] = [arr[index2], arr[index1]]);
console.log(arr);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 4:25









CertainPerformance

71.5k143453




71.5k143453












  • I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
    – David
    Nov 12 at 4:44










  • Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
    – David
    Nov 12 at 5:52


















  • I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
    – David
    Nov 12 at 4:44










  • Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
    – David
    Nov 12 at 5:52
















I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
– David
Nov 12 at 4:44




I will try this as soon as I get back home, I see what you mean, I used a very very simple version of my code as an example of what I was trying to do I know not to name it array was just example but I will try to implement this method into my own code will let you know how it goes, thank you
– David
Nov 12 at 4:44












Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
– David
Nov 12 at 5:52




Yes, thank you @CertainPerformance, by making my array of objects now allows me to make the connection between the array and users input so now I can switch them, at the bottom I get a bit fuzzy on how the whole switching works, I just used some if statements with an intermediate var because I didnt understand what the bottom half was doing. Though I will try to play around to try and understand it. Thank you for your help.
– David
Nov 12 at 5:52












up vote
0
down vote













Here's what your code might look like if you use an object:






    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);








share|improve this answer





















  • I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
    – David
    Nov 12 at 4:45










  • Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
    – David
    Nov 12 at 5:54















up vote
0
down vote













Here's what your code might look like if you use an object:






    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);








share|improve this answer





















  • I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
    – David
    Nov 12 at 4:45










  • Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
    – David
    Nov 12 at 5:54













up vote
0
down vote










up vote
0
down vote









Here's what your code might look like if you use an object:






    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);








share|improve this answer












Here's what your code might look like if you use an object:






    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);








    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);





    let arr = {};
arr['x1'] = '&spades';
arr['y1'] = '&clubs';
arr['z1'] = '&hearts';
arr['x2'] = ' ';
arr['y2'] = ' ';
arr['z2'] = ' ';
arr['x3'] = ' ';
arr['y3'] = ' ';
arr['z3'] = ' ';

console.log(arr['z1']);
arr['z1'] = 'foo';
console.log(arr['z1']);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 4:37









Jim B.

2,6411928




2,6411928












  • I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
    – David
    Nov 12 at 4:45










  • Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
    – David
    Nov 12 at 5:54


















  • I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
    – David
    Nov 12 at 4:45










  • Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
    – David
    Nov 12 at 5:54
















I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
– David
Nov 12 at 4:45




I see what you mean now CertainPerformace was saying something about the same will try as soon as I can, thank you
– David
Nov 12 at 4:45












Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
– David
Nov 12 at 5:54




Yes, @JimB. you were right the answer was that I had to make an array of objects. Thank you for your help.
– David
Nov 12 at 5:54


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255913%2fhow-find-a-value-within-an-array-with-a-users-input%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python