比较Javascript时返回两个数组中存在的对象的索引

时间:2016-11-08 15:32:28

标签: javascript arrays indexing

------被修改---------------

我有2个对象数组A,B

var a = [{id:1},{id:2},{id:3},{id:4},{id:5}];
var b = [{id:1},{id:2},{id:3}];

我想删除A和B中的对象

//after the operation
 a = [{id:1},{id:2},{id:3}];

我认为为了做到这一点,我需要在A中获取它们的索引以使用A.splice(),但我无法理解我是如何做到的。

这是我用来获取对象本身的代码 Difference between two array of objects in JavaScript

 var onlyInA = A.filter(function(current){
    return res.nodes.filter(function(current_b){
        return current_b._id == current._id
    }).length == 0

2 个答案:

答案 0 :(得分:2)

过滤器中的过滤器不是最佳选择。你想使用过滤器和一些。

var a = [{id:1},{id:2},{id:3},{id:4},{id:5}];
var b = [{id:1},{id:2},{id:5},{id:6},{id:7}];

var result = a.filter( function(oa) {
  return b.some( function(ob){
    return oa.id===ob.id;
  });
});

a.length = 0;
a.push.apply(a, result);

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

如果你想使用splice(),而不是向后循环。

var a = [{id:1},{id:2},{id:3},{id:4},{id:5}];
var b = [{id:1},{id:2},{id:5},{id:6},{id:7}];

for (var i=a.length-1;i>=0;i--) {
    var inB = b.some( function(ob){
      return a[i].id===ob.id;
    });
    if(!inB) {
      a.splice(i,1)
    }
}
console.log(a);

答案 1 :(得分:2)

您可以使用哈希表作为对象的ID,并使用它来过滤 public delegate void UpdatingTable(); private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Invoke(new UpdatingTable(DecodingData)); } private void cnvrtB_Click(object sender, EventArgs e) { progressBar1.Enabled = true; progressBar1.Visible = true; saveB.Visible = true; cnvrtB.Enabled = false; label5.Visible = true; backgroundWorker1.RunWorkerAsync(); //DecodingThread = new Thread(new ThreadStart(StartDecoding)); //DecodingThread.IsBackground = true; //if (!(DecodingThread.IsAlive) || DecodingThread == null) //{ // progressBar1.Enabled = true; // progressBar1.Visible = true; // saveB.Visible = true; // cnvrtB.Enabled = false; // label5.Visible = true; // Thread.Sleep(2000); // DecodingThread.Start(); //} //else if (DecodingThread.IsAlive) //{ // progressBar1.Enabled = true; // progressBar1.Visible = true; // saveB.Visible = true; // cnvrtB.Enabled = false; // label5.Visible = true; // DecodingThread.Resume(); //} } private void StartDecoding() { Thread.Sleep(1000); Invoke(new UpdatingTable(DecodingData)); } public void DecodingData() { try { //delete old records records.Clear(); //get records from converter if (VersionNumber == 1) { records.AddRange(LogEventDecode.getRecords()); } else if (VersionNumber == 0) { records.AddRange(LogEventDecode.getRecordsOld()); } //create datatable for records table = new System.Data.DataTable("data"); //create columns table.Columns.Add("Time", typeof(System.DateTime)); table.Columns.Add("Date", typeof(System.DateTime)); table.Columns.Add("dt", typeof(System.DateTime)); table.Columns.Add("User", typeof(System.String)); table.Columns.Add("SourceInformation", typeof(System.String)); table.Columns.Add("SourceType", typeof(System.String)); table.Columns.Add("SourceCondition", typeof(System.String)); table.Columns.Add("securityLevel", typeof(System.String)); table.Columns.Add("AdditionalInformation", typeof(System.String)); table.Columns.Add("RubCondition", typeof(System.String)); //populate datatable foreach (LogRecord r in records) { DataRow row = table.NewRow(); row["Time"] = r.Time; row["Date"] = r.Date; row["dt"] = r.dt; row["User"] = r.User; row["SourceInformation"] = r.SourceInformation; row["SourceType"] = r.SourceType; row["SourceCondition"] = r.SourceCondition; row["securityLevel"] = r.SecurityLevel; row["AdditionalInformation"] = r.AdditionalInformation; row["RubCondition"] = r.RubCondition; table.Rows.Add(row); //DecodingCount++; //label5.Text = "Decoded" + DecodingCount + " out of Total Logs " + records.Count; } view = new DataView(table); //bind to grid //DecodingThread.Abort(); } catch { MessageB.Show("Please make sure you have Imported the File \nOR\n The imported file is not corrupted.", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error); cnvrtB.Enabled = true; } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { logRecordBindingSource.DataSource = view; cnvrtB.Enabled = true; progressBar1.Visible = false; progressBar1.Enabled = false; label5.Visible = false; }

a