2017-05-24 330 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]」, 「普農」:9965148632, },{ 「stdname」: 「VETRI」, 「fathername」:「R 」 「MARK-1」:38.0, 「MARK2」:40.0, 「MARK3」:48.0, 「mark4」:28.0, 「mark5」:50.0, 「電子郵件」: 「[email protected]」 「phnum」:9962651490, } 。 。 。 ] – user7613910

回答

0

我覺得你不需要Lodash這裏只是用簡單的JS:

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