0

I have an array like this [HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"]. I got this from console.

I trying to find out value like below

var sku = skus_colorcode.indexOf('9620');                   
console.log(sku);

But it is returning -1.

Why I am getting this result ??

5
  • 1
    .indexOf looks only at numeric indices. Your Array has been populated with non-numeric properties, which will be ignored. So the question would be why an Array is being used this way in the first place. Commented Jul 2, 2016 at 15:58
  • 7
    That's not an array... Commented Jul 2, 2016 at 16:00
  • 2
    This is invalid array syntax. If you modify it to an object, you can use ES6 methods to get the key: Object.entries(skus_colorcode).find(a => a[1] == "9620")[0]. Commented Jul 2, 2016 at 16:01
  • I think the OP made it sufficiently clear that the syntax shown is the output from a console, which isn't required to represent data as valid JS syntax. Commented Jul 2, 2016 at 16:07
  • I think this must be an object not an array! Commented Jul 3, 2016 at 7:13

5 Answers 5

4

You code is not valid at all. An array is a list of elements, without keys. You must use an object, like this :

var skus_colorcode = {HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"}

To find the key (HT....) that corresponds to "9620", try this code :

var keys = Object.getOwnPropertyNames(skus_colorcode), key;

for(var i = 0; i < keys.length; i++)
    if(skus_colorcode[keys[i]] === "9620") {
        key = keys[i];
        break;
    }

// The right key is into the "key" variable
console.log(key); // says "HT2787Z2UK"
Sign up to request clarification or add additional context in comments.

6 Comments

An array can hold non-numeric keys and be perfectly valid. The syntax shown wouldn't be a valid initializer, but that's clearly not the actual syntax the OP used to construct the array.
On my browser (latest version of Chrome) the array cause a syntax error
"...The syntax shown wouldn't be a valid initializer, but that's clearly not the actual syntax the OP used to construct the array"
var a = []; a.foo = "bar"; console.log(a.foo); // bar
Yes, that syntax works, but not the array given in the question : [HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"] And an array is made to accept only numerical indexes, for other indexes you have to use an object.
|
1

Try this

function arraySearch(arr,val) {
    for (var key in arr) {
        this_val = array[key];
        if(this_val == val){
            return key;
            break;
        }
    }
 }

3 Comments

Your code has a big problem : Using a for(var ...) loop is VERY slow. You should use Object.keys(arr); and next do an incremental loop like I did to search through the array.
@ClementNerma: How do you think Object.keys is able to collect the keys? Object.keys also has to perform a .hasOwnProperty() check on each key. Did you actually test the performance?
Yes I've tested the performances. Object.keys() does not perform a .hasOwnProperty() because it's a C++ native function. So execution is faster than a JavaScript code. Strangely, for(var i in ...) is one of the slower way to explore an object. NOTE : I did an error that's not Object.keys() but Object.getOwnPropertyNames().
0

This should be

   var obj = {HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"}

for (var key of obj) {
  if (obj[key] == "9620"){
     return key;
  }
}

return false

Comments

0

I agree with "hemnath mouli" code should be like as he wrote:

<script type="text/javascript">
    keys = {HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"};
    function getIndexOf(obj,value){
        var count = 0;
        for (var i in obj){
            if(obj[i] == value.toString()){
                return  "index[" + count + "]:" + obj[i]  + " = " + i;
                //return what you want
            }
            count ++;
        }
    }
</script>

Then U got the values

<script type="text/javascript">
    alert(getIndexOf(keys,9621));
</script>

Could you please send a piece of code to retrieve the values @ClementNerma even I agree with you this is not the more efficient way.

I just do not want to "downvote" the question.

Or if you want to convert your Object to an Array:

<script type="text/javascript">
    keys = {HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"};
    function obj2Array(obj){
        k = [];
        for (var i in obj){
            k.push(obj[i]);
        }
        return k;
    }
</script>

<script type="text/javascript">
    arr = obj2Array(keys);
    alert (arr[2]);
</script>

@abu abu

Comments

-2

You can use Jquery:

var skus_colorcode = {HT2787UK: "9618", HT2787Z1UK: "9619", HT2787Z3UK: "9621", HT2787Z2UK: "9620"}


$.each( skus_colorcode , function( key, value ) {
   if(value == '9620')
       alert(key);
});

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.