1

I don't know what must be title for my question, I think it's so complicated. So, I have A array:

["87080207", "87101133", "91140156"]

And B Array:

 ["97150575", "97150575", "90141063"]

This B array, I put on html select value. Each of them(A and B array) is related. I need to show 87080207,87101133 (A array) when I choose value 97150575 (B array).
I have tried, but it didn't work.This is my code:

var a=[];
var b=[];
var arrayLength = dataComponentValuation.length;
for (var i = 0; i < arrayLength; i++) {
   a.push(dataComponentValuation[i].valuated);
   b.push(dataComponentValuation[i].valuator);
}
var ajoin = a.join();
var bjoin = b.join();
$('#valuatedEmpCompId_before').val(ajoin);
$('#valuator_before').val(bjoin);

In select, I put a function, this is it:

function emptyValuated() {
    var valby = $("#valBy").val(); //chosen value from select
    var b_valby = $("#valuator_before").val();

    var b_valuated = $("#valuatedEmpCompId_before").val();
    if(b_valby != ''){
        if(valby != b_valby)
        {
            $("#valuatedEmpCompId").val('');
        }
        else{            
             $("#valuatedEmpCompId").val(b_valuated);
        }

    }
    else{      

        $("#valuator_before").val(valby);
        $("#valuatedEmpCompId").val(b_valuated);
    }
}

Help me please...

2
  • Use Associative array to map array key with value in javascript. Commented Jun 9, 2016 at 8:23
  • If there is no logical or mathematical correlation between the items then you should utilize a Map object with keys and a values array holding all numbers related with the key. Commented Jun 9, 2016 at 8:30

2 Answers 2

1

As suggested, you could use an object as reference to the values of array A.

var arrayA = ["87080207", "87101133", "91140156"],
    arrayB = ["97150575", "97150575", "90141063"],
    object = Object.create(null);

arrayB.forEach(function (b, i) {
    object[b] = object[b] || [];
    object[b].push(arrayA[i]);
});

console.log(object);

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

Comments

0

I guess nowadays the Map object is a perfect solution for these jobs.

var arrayA = ["87080207", "87101133", "91140156"],
    arrayB = ["97150575", "97150575", "90141063"],
     myMap = arrayB.reduce((p,c,i) => p.has(c) ? p.set(c, p.get(c).concat(arrayA[i]))
                                               : p.set(c,[arrayA[i]])
                                               , new Map());
console.log(myMap.get("97150575"));
console.log(myMap.get("90141063"));

1 Comment

while i really appreciate your answers, your formatting makes it difficult (for me) to read it.

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.