简体   繁体   中英

loop between a nested object array and another array

I have two arrays, one it normal array and other is object array. All I want here is show me how to make a loop between both of them to get " Jhon " is good and sum of (midterm+finalterm) In a : the first no is no of school, the second is class>> i will use .split("|") to make them in an array In b: object array

I don't want the whole code, just the loop and if condition

the function will compare 1|1 in a with school_no and class_no to match

but I want if condition to color the name of the student based on evaluation like red for bad and blue for good.

here the arrays

 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); 

this is not a homework yet it's a small example of something bigger I'm working on.

Simply You have to iterate the data of a (student) and find the school no and class no in b (results):

 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> 

What I understood is you want to generate new array which contains list of object which contains string and your color based on evaluation.

 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); 

If they are in the same order, and the result[0] is always the one matching the student, you can do an ordinary loop and access by index:

 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"; } } 

You got an error on the 2nd array => school _no

I simply iterate throught a array. Each student item i searched for a node in b array, returning the node.result array.

 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; } } 

Here's how I'd do it with a more functional approach

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM