Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:56+00:00 2026-05-27T14:20:56+00:00

var arr1 = [12,’ss’,’sdd’,’sdd’,’kk’]; function unique(array){ var o = {},b = []; for(var i=0;i<array.length;i++){

  • 0
var arr1 = [12,'ss','sdd','sdd','kk'];
function unique(array){
  var o = {},b = [];
  for(var i=0;i<array.length;i++){
     if(!o[array[i]]){
       b.push(array[i]);
       o[array[i]] = true;
     }
  }
  return b;
}
unique(arr1) //It works fine .output [12,'ss','sdd','kk']

but,it has some issues on arr2 below:

var arr2 =  [12,'ss','sdd','sdd','kk','12'];//output [12,'ss','sdd','kk']

does it make wrong?I think it should output [12,’ss’,’sdd’,’kk’,’12’],can we fixed this promble?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:20:56+00:00Added an answer on May 27, 2026 at 2:20 pm

    Key names are always converted to a string. I recommend to use Array.prototype.indexOf in order to check whether an array entry is unique or not. The indexOf method also behaves correctly regarding objects[1].

    Demo: http://jsfiddle.net/YE9jx/

    function unique(array){
      var b = [];
      for(var i=0; i<array.length; i++){
         if(b.indexOf(array[i]) == -1) b.push(array[i]);
      }
      return b;
    }
    

    [1] The correct behaviour is: Different if the object references are different:

    var obj1 = [1,2];
    var obj2 = [1,2];
    unique([obj1, obj2]);  //[[1,2], [1,2]]  // Because they're different arrays
    unique([obj1, obj1]);  //[[1,2]]         // Because both elements are obj1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var arr1:Array = new Array(); arr1.push(item1); arr1.push(item2); arr1.push(item3); then arr1 and its elements get
I have an array like this: var arr1 = [a, b, c, d]; How
Example: var arr_1:Array = new Array(); arr_1.push({sdate:2010-02-02,status:New}); arr_1.push({sdate:2010-02-03,status:New}); arr_1.push({sdate:2010-02-04,status:New}); arr_1.push({sdate:2010-02-05,status:New}); How can i change
var thumbs = document.getElementsByTagName(img); for (var i=0; i<thumbs.length; i++) { Core.addEventListener(thumbs[i], click, function() {alert(i);});
I have an array of n number of items var arr1 = [2, 0,
var y = new w(); var x = y.z; y.z= function() { doOneThing(); x();
How to merge multiple (5 arrays in one): var arr1 = ['a','b']; var arr2
var arr = new Array(); arr[0] = 'Departmental News' arr[1] = 'Departmental News' arr[2]
Currently, I am creating a 3d array in js using the following: var arr
I can't find any information on this, but suppose I have var arr =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.