2017-05-24 6 views
0

아래에 두 개의 JSON 배열 객체가 있습니다. stdcontinfo JSON 값과 일치하는 stdname을 가져 와서 stdJson의 해당 JSON에 추가하려고합니다.Lodash를 사용하여 두 개의 다른 배열에서 동일한 값을 얻는 방법

누구든지 나를 도와 드릴까요?

var stdJson = [{ 
     "_id": ObjectId("5923ed1f610be38692e88e4d"), 
     "stdname": "Bala", 
     "fathername": "Kannappan", 
     "mark1": 78.0, 
     "mark2": 70.0, 
     "mark3": 98.0, 
     "mark4": 88.0, 
     "mark5": 100.0 
    }, 
    { 
     "_id": ObjectId("5923ed99610be38692e88e4e"), 
     "stdname": "vetri", 
     "fathername": "R", 
     "mark1": 38.0, 
     "mark2": 40.0, 
     "mark3": 48.0, 
     "mark4": 28.0, 
     "mark5": 50.0 
    }, 
    { 
     "_id": ObjectId("5923edf6610be38692e88e4f"), 
     "stdname": "kumba", 
     "fathername": "K", 
     "mark1": 68.0, 
     "mark2": 60.0, 
     "mark3": 68.0, 
     "mark4": 88.0, 
     "mark5": 35.0 
    }, 
    { 
     "_id": ObjectId("5923ee1a610be38692e88e50"), 
     "stdname": "Jagadesh", 
     "fathername": "P", 
     "mark1": 100.0, 
     "mark2": 90.0, 
     "mark3": 89.0, 
     "mark4": 100.0, 
     "mark5": 95.0 
    }, { 
     "_id": ObjectId("5923ee6b610be38692e88e51"), 
     "stdname": "Dayakar", 
     "fathername": "R", 
     "mark1": 70.0, 
     "mark2": 80.0, 
     "mark3": 69.0, 
     "mark4": 60.0, 
     "mark5": 55.0 
    } 
] 

var stdcontinfo = [{ 
     "_id": ObjectId("592437891a87641edc58547e"), 
     "stdname": "dev1", 
     "email": "[email protected]", 
     "phnum": 6545645645.0, 
     "__v": 0 
    }, 
    { 
     "_id": ObjectId("5924ffe6837e5d2224f42e00"), 
     "stdname": "Bala", 
     "email": "[email protected]", 
     "phnum": 9965148632.0, 
     "__v": 0 
    }, 
    { 
     "_id": ObjectId("592500371e469423a4f65306"), 
     "stdname": "kumba", 
     "email": "[email protected]", 
     "phnum": 9962651490.0, 
     "__v": 0 
    }, 
    { 
     "_id": ObjectId("59250085f02d4c0face17820"), 
     "stdname": "vetri", 
     "email": "[email protected]", 
     "phnum": 9962651490.0, 
     "__v": 0 
    }, 
    { 
     "_id": ObjectId("592500c6c6a6af1d6c7c3fa5"), 
     "stdname": "Jagadesh", 
     "email": "[email protected]", 
     "phnum": 1234567890, 
     "__v": 0 
    } 
] 
+0

내 예상 출력 :VAR의 stdJson = [{ "stdname": "발라" "fathername": "Kannappan" "MARK1"78.0, "MARK2"70.0, "mark3"98.0, "mark4" 88.0, "mark5": 100.0, "이메일": "[email protected]" "phnum": 9965148632, }, { "stdname": "VETRI" "fathername": "R " "MARK1 ": 38.0, "MARK2 ": 40.0, "mark3 ": 48.0, "mark4 ": 28.0, "mark5 ": 50.0, "이메일 ":"[email protected] " "phnum": 9962651490, } . . ] – user7613910

답변

0

난 당신이 단순한 JS를 사용하여 여기 Lodash 필요하지 않습니다 생각 : 당신이 교차 기능 중 하나를 사용할 수 있습니다 lodash와

stdJson.forEach(itemA=>{ 
    let resItem = stdcontinfo.find(itemB=>itemA.stdname === itemB.stdname); 
    if(resItem){ 
     itemA.email = resItem.email; 
     itemA.phnum = resItem.phnum; 
    } 
});