2

My array looks like this.

var qnsAnsArray = [];
var qnAnswer = { qn: a, points: curretnAnswerPoint };
qnsAnsArray.push(qnAnswer);

and the value getting in the array is

qnsAnsArray =[
    Object { qn=0, points="2"}, 
    Object { qn=1, points="2"}, 
    Object { qn=2, points="6"}, 
    Object { qn=2, points="2"}, 
    Object { qn=3, points="3"}
]

how will i get the duplicate 'qn' value by using jQuery? Each time when I change answer, I wants to update answer points instead of insertion into the array.

Thanks in advance.

Nivya M
  • 102
  • 1
  • 1
  • 7
  • 1
    Do you want true/false? or... what do you want? – taesu Jan 30 '15 at 05:30
  • 2
    whats your try? And what do you mean by dubplicate qn value? – Amit Joki Jan 30 '15 at 05:30
  • I have got 10 question and 5 answers for each qn with answer points. When I go to the previous question , If I am changing the answer I wanted to replace with new answer points. – Nivya M Jan 30 '15 at 05:38
  • http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Dom Jan 30 '15 at 05:49
  • your question isn't clear enough, consider revising – random-forest-cat Jan 30 '15 at 06:08
  • Each time when I change answer, I wants to update answer points instead of insertion into the array. – Nivya M Jan 30 '15 at 06:18
  • So the example in your question shouldn't actually happen in the first place because you want to update the existing qn 2 rather than add another? If you have a concept of "previous question" as per your comment don't you have an index variable for the current question? Alternatively, don't use an array, store the data in an object using the question number as key. – nnnnnn Jan 30 '15 at 06:20
  • i think the user just wants to identify dup keys in array, but question isnt clear enough. i have no idea what an answer point is – random-forest-cat Jan 30 '15 at 06:27
  • I am getting question,answer and answer points from a xml file. Each question followed by 5 answers. There is no correct or wrong answer. It is a survey.And points are different for different answer. – Nivya M Jan 30 '15 at 06:32

3 Answers3

4

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.Here we copy each item to another variable and checks it.Hope this helps u mate.. :)

var array = [{qn : 0, points : "2"}, {qn : 1, points :"2"}, {qn : 2, points : "6"}, {qn : 2, points : "2"}, {qn : 3, points : "3"}]
var temp = [];
$.each(array, function (key, value) {
   if($.inArray(value.qn, temp) === -1) {
        temp.push(value.qn);
    }else{
       console.log(value.qn+" is a duplicate value");
    }
});

Fiddle

FYI

jQuery.inArray()

Nibin
  • 3,922
  • 2
  • 20
  • 38
0

You can try if this works for you:

var arrayToTest = [{ qn:1, points:"2"}, { qn:1, points:"2"}, { qn:2, points:"6"}, { qn:2, points:"2"}, { qn:3, points:"3"}]
var uniqueQns = {};
var uniqueQnObj = [];


$.each(arrayToTest, function(i, ele) {

    if (!uniqueQns[ele.qn]) {
        uniqueQns[ele.qn] = true;
        uniqueQnObj.push(ele);
    } else {
        uniqueQns[ele.qn] = false;
    }
});

After running this, uniqueQns will contain : Object {1: false, 2: false, 3: true}. 1, 2, 3 represent qn and their corresponding value will depict it the qn is unique or not. 1: false means that qn1 has more than one occurrences.

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0
function find_duplicate_value(objList, field) {

    if (objList.length == 0) 
        return null;

    var tmp = [];

    for (var i = 0; i < objList.length; i++) {
        var val = objList[i][field];
        var pos = tmp.indexOf(val)

        if (pos > -1) 
            return pos;

        tmp.push(val);
    }
    return null;
}

Is this what u want?

http://jsfiddle.net/Ly45g952/

Parfait
  • 1,752
  • 1
  • 11
  • 12