在嵌套对象数组和另一个数组之间循环

时间:2018-10-04 09:06:31

标签: javascript arrays json

我有两个数组,一个是普通数组,另一个是对象数组。  我想在这里告诉我如何在两者之间循环以使“ Jhon ”达到和(中期+最终期限)的总和 在一个 : 第一个是学校,第二个是班>>我将使用.split(“ |”)将它们排列成数组 在b:对象数组

  

我不需要整个代码,只需要循环和条件

该函数会将a中的1 | 1与school_no和class_no进行比较

但我想根据条件将学生的名字涂上颜色,例如红色表示不好,蓝色表示很好。

这里是数组

a=["1|1|Jhon","1|2|Akram","1|3|Mali"]
    //first no for school no
    //second no for class no.
    b= [{
        "result": [
          {
            "midterm": 25,
            "evaluation": "good",
            "finalterm": 24
          }
        ],
        "school  _no": 1,
        "class_no": 1
      },
      {
        "result": [
          {
            "midterm": 55,
            "evaluation": "verygood",
            "finalterm": 60
          }
        ],
        "school_no": 1,
        "class_no": 2
      },
    {
        "result": [
          {
            "midterm": 11,
            "evaluation": "bad",
            "finalterm": 12
          }
        ],
        "school_no": 1,
        "class_no": 3
      }
    
    ];
    console.log(a);
    console.log(b);

这不是家庭作业,但这只是我正在从事的更大工作的一个小例子。

5 个答案:

答案 0 :(得分:0)

您只需对a(学生)的数据进行迭代,并在b(结果)中找到学校编号和班级编号:

a=["1|1|Jhon","1|2|Akram","1|3|Mali"]
//first no for school no
//second no for class no.
b= [{
    "result": [
      {
        "midterm": 25,
        "evaluation": "good",
        "finalterm": 24
      }
    ],
    "school_no": 1,
    "class_no": 1
  },
  {
    "result": [
      {
        "midterm": 55,
        "evaluation": "verygood",
        "finalterm": 60
      }
    ],
    "school_no": 1,
    "class_no": 2
  },
{
    "result": [
      {
        "midterm": 11,
        "evaluation": "bad",
        "finalterm": 12
      }
    ],
    "school_no": 1,
    "class_no": 3
  }

];
var result=[]
a.forEach(function(element) {
  var data= element.split("|");
 
  var found = b.find(function(element) {
  return element.school_no == data[0] && element.class_no== data[1];
});
 result.push(data[2]+" is "+found.result[0].evaluation+" and sum of (midterm+finalterm) is :"+(found.result[0].midterm+found.result[0].finalterm));
 document.getElementById(data[2]).className = found.result[0].evaluation;

});
console.log(result);
.good {
    color: blue;
}

.bad {
    color: red;
}

.verygood {
    color: green;
}
<div id="Jhon">Jhon</div>
<div id="Akram">Akram</div>
<div id="Mali">Mali</div>

答案 1 :(得分:0)

我了解的是您想生成一个新数组,该数组包含基于评估的对象列表,该对象列表包含字符串和您的颜色。

var a=["1|1|Jhon","1|2|Akram","1|3|Mali"]
//first no for school no
//second no for class no.
var b= [{
"result": [
  {
    "midterm": 25,
    "evaluation": "good",
    "finalterm": 24
  }
],
"school_no": 1,
"class_no": 1
  },
  {
"result": [
  {
    "midterm": 55,
    "evaluation": "verygood",
    "finalterm": 60
  }
],
"school_no": 1,
"class_no": 2
  },
{
"result": [
  {
    "midterm": 11,
    "evaluation": "bad",
    "finalterm": 12
  }
],
"school_no": 1,
"class_no": 3
  }
];


var resultArrNew = a.reduce(function(ac, val) {
    var valArra = val.split("|");
var matched = b.filter(function(obj) {
    return obj.school_no == valArra[0] && obj.class_no == valArra[1];
});
var text = valArra[2] + " is " + matched[0].result[0].evaluation + " and sum is " + (matched[0].result[0].midterm+matched[0].result[0].finalterm);
var color = matched[0].result[0].evaluation === 'good' ? 'blue' : 'red';

ac.push({text: text, color: color});
return ac;
}, []);
console.log(resultArrNew);

答案 2 :(得分:0)

如果它们的顺序相同,并且result[0]始终是与学生匹配的那个,则可以执行普通循环并按索引访问:

const students = [ "1|1|Jhon",  "1|2|Akram","1|3|Mali" ];
const results = [{result:[{midterm:25,evaluation:"good",finalterm:24}],school_no:1,class_no:1},{result:[{midterm:55,evaluation:"verygood",finalterm:60}],school_no:1,class_no:2},{result:[{midterm:11,evaluation:"bad",finalterm:12}],school_no:1,class_no:3}];


for (let i = 0; i < students.length; i += 1) {
  const student = students[i];
  const result = results[i];
  
  console.log({
    name: parseStudentName(student),
    result: getResultColor(result),
  });
}

function parseStudentName(studentString) {
  return studentString.split("|")[2]
};

function getResultColor(result) {
  switch(result.result[0].evaluation) {
    case "verygood":
    case "good":
      return "blue";
    case "bad":
      return "red";
  }
}

答案 3 :(得分:0)

您在第二个数组上出错=> school _no

我只是遍历a数组。我在每个学生项目中搜索b数组中的一个节点,并返回node.result数组。

a=["1|1|Jhon","1|2|Akram","1|3|Mali"]
//first no for school no
//second no for class no.
b= [{
    "result": [
      {
        "midterm": 25,
        "evaluation": "good",
        "finalterm": 24
      }
    ],
    "school_no": 1,
    "class_no": 1
  },
  {
    "result": [
      {
        "midterm": 55,
        "evaluation": "verygood",
        "finalterm": 60
      }
    ],
    "school_no": 1,
    "class_no": 2
  },
{
    "result": [
      {
        "midterm": 11,
        "evaluation": "bad",
        "finalterm": 12
      }
    ],
    "school_no": 1,
    "class_no": 3
  }
];

for(var i = 0; i < a.length; i++) {
    var parts = a[i].split('|');
    try {
        var node = getStudentNode(parseInt(parts[0]), parseInt(parts[1]));
        console.log(node);
    }
    catch(ex){
        console.log(ex);
    }
}

function getStudentNode(sc, cl) {
    for(var i = 0; i < b.length; i++) {
        var node = b[i];
        if(node.school_no !== sc || node.class_no !== cl) continue;
        return node.result;
    }
}

答案 4 :(得分:0)

这就是我将采用更实用的方法的方法

const a =["1|1|Jhon", "1|2|Akram", "1|3|Mali"]

const b = [{
    "result": [
      {
        "midterm": 25,
        "evaluation": "good",
        "finalterm": 24
      }
    ],
    "school  _no": 1,
    "class_no": 1
  },
  {
    "result": [
      {
        "midterm": 55,
        "evaluation": "verygood",
        "finalterm": 60
      }
    ],
    "school_no": 1,
    "class_no": 2
  },
{
    "result": [
      {
        "midterm": 11,
        "evaluation": "bad",
        "finalterm": 12
      }
    ],
    "school_no": 1,
    "class_no": 3
  }
];

// import some functional helpers
const { map, propPathOr, compose } = crocks

const getStudent = record => {
  const [school_no, class_no, student_name] = record.split('|')
  return { school_no, class_no, student_name }
}

const getStudents = map(getStudent)

const getEvaluation = propPathOr('', ['result', 0, 'evaluation'])

const getEvaluations = map(getEvaluation)

// assuming your lists are sorted
const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]])

const combineStudentAndEvaluation = ([student, evaluation]) =>
  ({ ...student, evaluation })

const stringify = ({student_name, evaluation}) =>
  `${student_name}: ${evaluation}`

const studentEvaluationToString = compose(stringify, combineStudentAndEvaluation)

const merged = zip(getStudents(a), getEvaluations(b))
  .map(studentEvaluationToString)
    
console.log(merged)
<script src="https://unpkg.com/crocks/dist/crocks.min.js"></script>