比较两个数组时如何更改数组中的值

时间:2018-06-27 05:30:40

标签: javascript underscore.js

var array1 = [ {"name":"sam","surname":"sameera","location":"mumbai","age":"22"}];
var array2 =[ {"name":"SAM","surname":"SAMEERA"}];

我希望输出像这样:

[{"name":"SAM","surname":"SAMEERA","location":"mumbai","age":"22"}];

使用javascript和underscorejs

我的尝试如下:

var workingValues = _.filter(array1, function(item){ return item});
console.log(workingValues); 
var validItems = []; 

for(var i =0 ; i < array2.length; i++){ 
  var item = array2[i]; 
  console.log(item); 

  var findItems = _.filter(array1,function(ite) {
    return ite.name.toUpperCase() == item.name.toUpperCase() && ite.surname.toUpperCase() == item.surname.toUpperCase()}); 
  validItems.push(item); 
  console.log(findItems); 
}

4 个答案:

答案 0 :(得分:1)

var array1 = [ {"name":"sam","surname":"sameera","location":"mumbai","age":"22"}];
var array2 =[ {"name":"SAM","surname":"SAMEERA"}];

var result = [{...array1[0],...array2[0]}];
console.log(result);

答案 1 :(得分:0)

根据您提供的有限数据。

var array1 = [ {"name":"sam","surname":"sameera","location":"mumbai","age":"22"}];
var array2 =[ {"name":"SAM","surname":"sameera"}];
console.log( _.extend(array2, array1))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

答案 2 :(得分:0)

您可以尝试

[Import]
public IParentOrdersSLViewModel ViewModel
{
    get
    {
        return DataContext as IParentOrdersSLViewModel;
    }
    set
    {
        DataContext = value;
        IParentOrderView pv = ViewModel.View;
        pv.uc = this.lvParentOrdersSL;

    }
}

答案 3 :(得分:-1)

var array1 = [{
    "name":"sam",
    "surname":"sameera",
    "location":"mumbai",
    "age":"22"
}];
var array2 =[{
    "name":"SAM",
    "surname":"sameera"
}];
var mergearray= array1 .concat(array2);