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.

9
  • 1
    Do you want true/false? or... what do you want? Commented Jan 30, 2015 at 5:30
  • 2
    whats your try? And what do you mean by dubplicate qn value? Commented Jan 30, 2015 at 5: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. Commented Jan 30, 2015 at 5:38
  • stackoverflow.com/questions/9229645/… Commented Jan 30, 2015 at 5:49
  • your question isn't clear enough, consider revising Commented Jan 30, 2015 at 6:08

3 Answers 3

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()

Sign up to request clarification or add additional context in comments.

2 Comments

we can't assume the questioner is using jquery
he has tagged it in jquery isn't it mate..??
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.

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.