0

what I am really trying to do is add an array after an event only if that event occurred once like the press of the enter button. if the user keeps on entering the wrong answer it should only update the array once with the array that represents the question. if you can help me out with this question that deals with more of what i am trying to do would be great.

if you cant help me with that i am wondering how to insert an array after an event only if the array isn't in the array. can you show me some code where you search for an array inside of an array and if that array is inside do something or nothing?

I don't understand why in the below code i am not able to search for arrays

function onlyUnique(value, index ,self){
     return self.indexOf(value) === index;
}
  var a =  ['a' , [1], [1],'a']
  var unique = a.filter(onlyUnique)
  console.log(unique)
Community
  • 1
  • 1
jack blank
  • 5,073
  • 7
  • 41
  • 73
  • `indexOf` performs value equality, not object equality, so `[] !== []` – elclanrs Jul 08 '15 at 00:15
  • so what do i do? what is a good way to check if arrays are equal. I want to do this because i might want to push into an array an array that is not already in the array dynamically. – jack blank Jul 08 '15 at 00:20
  • keep track of last entered value for each question and compare there or disable UI until user changes something – charlietfl Jul 08 '15 at 00:37

2 Answers2

0

You could use an object like an associative array. You would need some way to identify the questions to link to the answers.

// answers storage
var answers = {};

// set answer to question 1, this would be replaced by automation
var questionId = 'q1';
var data       = 'some answer';

// insert if not present
if(answers[questionId] === undefined) {
   answers[questionId] = data;
}

// manipulate existing answer
else {
   // do something with answers[questionId]
}
spenibus
  • 4,339
  • 11
  • 26
  • 35
0

There is not a simple operation to do a deep comparison of objects. However, you can try converting the objects to JSON and comparing - provided the JSON generated has the objects properties in the same order then it will generate identical strings which can be compared:

function onlyUnique(a){
  return a.map(function(v){ return JSON.stringify(v); })
          .sort()
          .filter(function(v,i,a){ return i==0 || v !== a[i-1]; })
          .map(function(v){return JSON.parse(v);});
};

var a =  ['a' , [1], [1],'a']
var unique = onlyUnique(a);
console.log(unique)
MT0
  • 143,790
  • 11
  • 59
  • 117
  • thanks for your reply. I think i figured something out you seem smart so.let me know if Im on the right track. I figure I cant compare arrays because there always false `[] == []` is false. So i converted them to strings and i compared them. if the string are equal then don't insert the string into the array. that way i will get unique data on event. I think that's why you used JSON.stringify. to compare string not arrays? – jack blank Jul 08 '15 at 01:09
  • Correct - `JSON.stringify()` will give you a string representation of an object (including nested objects) which can then be compared for equality (the `sort()` orders the array of JSON and then `filter()` de-duplicates) before converting the JSON back to an object using `JSON.parse()` . Based on your example of and array of strings or arrays containing simple types then this should be sufficient - however if you have more complex objects then you should test this thoroughly to ensure it does what you expect. – MT0 Jul 08 '15 at 01:31
  • 1
    This might be worth a look: http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript – spenibus Jul 08 '15 at 01:38