匹配两个对象数组之间的特定属性,然后将值插入一个对象

时间:2016-10-01 08:38:43

标签: javascript arrays

我有两个对象数组,如 -

var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}]
var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}]

我希望将Content中的arrayOne值与Content_Key中的arrayTwo值进行匹配。如果匹配来自arrayTwo的{​​{1}}更新ValueContent。所以更新的数组看起来像 -

arrayOne

如何做到这一点的任何线索?目前我能够匹配部分,但超出了我的能力。

5 个答案:

答案 0 :(得分:4)

如果您为此使用某种哈希,性能会得到优化。正如其他人所示,你可以为此创建一个对象,但这是一个ES6 Map的解决方案,它作为thisArg传递给forEach

arrayTwo.forEach(function (el) {
    el.ValueContent = this.get(el.Content_Key);
}, new Map(arrayOne.map(el => [el.Content, el.ValueContent])));



var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}];
var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}];

arrayTwo.forEach(function (el) {
    el.ValueContent = this.get(el.Content_Key);
}, new Map(arrayOne.map(el => [el.Content, el.ValueContent])));

console.log(arrayTwo);




备注

您使用第一个字符大写的属性名称。您可能需要考虑以小写字母开头,因为有a convention来保留构造函数(或ES6中的类)的初始大写字母。

答案 1 :(得分:0)

您可以为// // ViewController.swift // StackOverflow // // Created by Seoksoon Jang on 2016. 10. 1.. // Copyright © 2016년 Seoksoon Jang. All rights reserved. // import UIKit class ViewController: UIViewController { var buttonTagNumberArray : Array<Int>? var randomIndex : Int? @IBOutlet var button1: UIButton! @IBOutlet var button2: UIButton! @IBOutlet var button3: UIButton! @IBOutlet var button4: UIButton! @IBOutlet var button5: UIButton! @IBOutlet var button6: UIButton! @IBAction func button1Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button1.tag) { button1Action(button1) } else { button1.isHidden = true switch randomIndex! { case button1.tag : print("it should happen : \(button1.tag)") break case button2.tag : button2.isHidden = false; break case button3.tag : button3.isHidden = false; break case button4.tag : button4.isHidden = false; break case button5.tag : button5.isHidden = false; break case button6.tag : button6.isHidden = false; break default: // break; } return ; } } @IBAction func button2Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button2.tag) { button2Action(button2) } else { button2.isHidden = true; switch randomIndex! { case button1.tag : button1.isHidden = false; break case button2.tag : print("it should happen : \(button2.tag)") break case button3.tag : button3.isHidden = false; break case button4.tag : button4.isHidden = false; break case button5.tag : button5.isHidden = false; break case button6.tag : button6.isHidden = false; break default: // break; } return ; } } @IBAction func button3Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button3.tag) { button3Action(button3) } else { button3.isHidden = true; switch randomIndex! { case button1.tag : button1.isHidden = false; break case button2.tag : button2.isHidden = false; break case button3.tag : print("it should happen : \(button2.tag)") break case button4.tag : button4.isHidden = false; break case button5.tag : button5.isHidden = false; break case button6.tag : button6.isHidden = false; break default: // break; } return ; } } @IBAction func button4Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button4.tag) { button4Action(button4) } else { button4.isHidden = true; switch randomIndex! { case button1.tag : button1.isHidden = false; break case button2.tag : button2.isHidden = false; break case button3.tag : button3.isHidden = false; break case button4.tag : print("it should happen : \(button2.tag)") break case button5.tag : button5.isHidden = false; break case button6.tag : button6.isHidden = false; break default: // break; } return ; } } @IBAction func button5Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button5.tag) { button5Action(button5) } else { button5.isHidden = true; switch randomIndex! { case button1.tag : button1.isHidden = false; break case button2.tag : break case button3.tag : button3.isHidden = false; break case button4.tag : button4.isHidden = false; break case button5.tag : print("it should happen : \(button2.tag)") break case button6.tag : button6.isHidden = false; break default: // break; } return ; } } @IBAction func button6Action(_ sender: AnyObject) { randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count))) if (randomIndex! == button6.tag) { button6Action(button6) } else { button6.isHidden = true; switch randomIndex! { case button1.tag : button1.isHidden = false; break case button2.tag : button2.isHidden = false; break case button3.tag : button3.isHidden = false; break case button4.tag : button4.isHidden = false; break case button5.tag : button5.isHidden = false; break case button6.tag : print("it should happen : \(button2.tag)") break default: // break; } return ; } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. buttonTagNumberArray = [button1.tag, button2.tag, button3.tag, button4.tag, button5.tag, button6.tag] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } // class end 使用哈希表,并将arrayOne应用于相应的对象(如果存在)。

&#13;
&#13;
ValueContent
&#13;
var arrayOne = [{ "Content": 1, "ValueContent": 2 }, { "Content": 3, "ValueContent": 4 }],
    arrayTwo = [{ "Ex": "x", "ValueNum": 20, "Content_Key": 3 }, { "Ex": "y", "ValueNum": 10, "Content_Key": 1 }],
    hash = Object.create(null);

arrayOne.forEach(function (a) {
    hash[a.Content] = a;
});

arrayTwo.forEach(function (a) {
    if (hash[a.Content_Key]) {
        a.ValueContent = hash[a.Content_Key].ValueContent;
    }
});

console.log(arrayTwo);    
&#13;
&#13;
&#13;

答案 2 :(得分:0)

首先,我将在arrayTwo内创建一个对象索引,其中密钥为Content_Key。也就是说,我将构建一个属性为Content_Key的对象,并为整个数组中的对象赋值。

因此,我可以轻松地通过Content_Key找到对象。

最后,我将使用整个对象索引迭代arrayOne中的每个项目以向其添加ValueContent属性:

var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}];
var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}];

var arrayTwoIndex = arrayTwo.reduce(function(result, item) {
  result[item.Content_Key] = item;
  
  return result;
}, {});

arrayOne.forEach(function(item) {
   // This approach of using an object index is very powerful because
   // otherwise you would need to iterate arrayTwo for each arrayOne
   // iteration, and this way you save a lot of CPU cycles because 
   // you're directly accessing to arrayTwo objects by Content_Key!
   arrayTwoIndex[item.Content].ValueContent = item.ValueContent;
});

console.log(JSON.stringify(arrayTwo));

答案 3 :(得分:0)

尝试这种简单的方法(将复杂性降低到O(n+m),其中narrayOne中的项目数,marrayTwo中的项目数)

var arrayOne = [{"Content":1, "ValueContent":2},{"Content":3, "ValueContent":4}];
var arrayTwo = [{"Ex": "x", "ValueNum":20,"Content_Key":3}, {"Ex":"y","ValueNum":10,"Content_Key": 1}];

//prepare map from arrayone for Content value
var map = {};
arrayOne.forEach( function( item ){
  map[ item[ "Content" ] ] = item[ "ValueContent" ];
});

//Now iterate arrayTwo to see if there is a match
arrayTwo = arrayTwo.map( function( item ){
  var contentKey = item[ "Content_Key" ];
  if ( map[ contentKey ] != undefined )
  {
      item[ "ValueContent" ] = map[ contentKey ];
  }
  return item;
});

console.log( arrayTwo );

答案 4 :(得分:0)

您可以使用2 forEach方法

arrayTwo.forEach(obj2 => { arrayOne.forEach(obj1 => {
if (obj2.Content_Key === obj1.Content)  obj2.ValueContent = obj1.ValueContent;
})});
console.log(arrayTwo); //[ { Ex: 'x', ValueNum: 20, Content_Key: 3, ValueContent: 4 }, { Ex: 'y', ValueNum: 10, Content_Key: 1, ValueContent: 2 } ]