Home  >  Article  >  Web Front-end  >  How to use js to merge arrays to generate key:value array

How to use js to merge arrays to generate key:value array

php中世界最好的语言
php中世界最好的语言Original
2018-05-30 15:06:316801browse

This time I will show you how to use js to merge arraysGenerate key:value array, use js to merge arrays to generate key:value arrayWhat are the precautions, the following is the actual combat Let’s take a look at the case.

Core Code

// var activeSubjectsName = ["语文", "数学", "英语", "思想品德", "科学"];
// var activeSubjectsNum = [46, 2, 2, 28, 29];
// var activeSubjectsArr = [];
for (var i = 0; i < activeSubjectsName.length; i++) {
 console.log(i);
 var activeSubjectsObject = {};
 for (var j = 0; j < activeSubjectsNum.length; j++) {
  if (i == j) {
   activeSubjectsObject.name = activeSubjectsName[i];
   activeSubjectsObject.value = activeSubjectsNum[j];
   activeSubjectsArr.push(activeSubjectsObject);
  }
 }
}
console.log(activeSubjectsArr);
// activeSubjectsArr=[
// {name: "语文", value: 46},
// {name: "数学", value: 2},
// {name: "英语", value: 2},
// {name: "思想品德", value: 28},
// {name: "科学", value: 29}
// ]

The principle is to combine two arrays into one array, which is the same as the json comparison relationship. Friends who need it can refer to it. Note that the two arrays can be matched in order. .

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

jQuery implementation of fuzzy query practical case analysis

How to deal with async/await wasteful performance issues

The above is the detailed content of How to use js to merge arrays to generate key:value array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn