我正在努力实现以下目标:
使用两个第一个数组的交集创建一个新数组
下式给出:
array1: [{ 'id': 2, 'detail': 2 },
{ 'id': 4, 'detail': 5}]
array2:[{ 'no': 1},
{ 'no': 4,},
{'no' : 7}]
GENERATE:
array3: [{ 'id': 4, 'detail': 5}]
Because array1.id = array2.no
有关清洁/短代码的任何建议请
答案 0 :(得分:0)
您可以通过查找所需的uses
CNClrLib.Host, CNClrLib.Core, CNClrLib.Host.Utils, CNClrLib.Collections,
CNClrLib.System, CNClrLib.IO, CNClrLib.Runtime, CNClrLib.Xml,
CNClrLib.Drawing, CNClrLib.TypeNames, SDK.NET.Interface_Enums,
SDK.NET.Interface_Const;
type
{ Forward Declarations }
TIVariable = Class;
TIFieldList = Class;
TIDocumentRevisionArray = Class;
TIDocument = Class;
TIField = Class;
TILabelObjectList = Class;
TILabelSettings = Class;
TIPrintSettings = Class;
TIVariableList = Class;
TILabel = Class;
TILabelPreviewSettings = Class;
TIPrintRequest = Class;
TIPrintToGraphicsSettings = Class;
TIPrintToGraphicsResult = Class;
TILabelObject = Class;
TIPageLayout = Class;
TIControlCenterProperties = Class;
TIPrintServiceSettings = Class;
TIDocumentStorage = Class;
TISolution = Class;
TSDKException = Class;
TIPrintEngine = Class;
...
{ TILabel }
TILabel = Class(TClrBaseObject)
private
function Get_Author: String;
function Get_DataSets: _GenericList;
function Get_Description: String;
function Get_Directory: String;
function Get_FileName: String;
function Get_FilePath: String;
function Get_LabelObjects: TILabelObjectList;
function Get_LabelSettings: TILabelSettings;
function Get_PrintSettings: TIPrintSettings;
function Get_Revision: Integer;
function Get_Title: String;
function Get_Variables: TIVariableList;
public
constructor Create(AObjVar: OleVariant);
function Clone: TILabel;
procedure CreateLabelVariant(outputFileName: String; printTimeDataSourceNames: _StringArray);
function CreateXMLLabelInformation: _XDocument;
procedure EndSessionPrint(sessionPrintRequest: TISessionPrintRequest);
function GetLabelPreview(settings: TILabelPreviewSettings): Olevariant;
function Print(quantity: Integer): TIPrintRequest;
function PrintAll: TIPrintRequest;
function PrintAllAsync: TIPrintRequest;
function PrintAsync(quantity: Integer): TIPrintRequest;
function PrintToGraphics(settings: TIPrintToGraphicsSettings): TIPrintToGraphicsResult;
procedure SaveAs(fileName: String; overwrite: Boolean);
procedure SessionPrint(quantity: Integer; sessionPrintRequest: TISessionPrintRequest);
procedure SetVariableValues(variablesList: String; ignoreMissingVariables: Boolean);
procedure SetVariableValuesXml(variablesList: String; ignoreMissingVariables: Boolean);
function StartSessionPrint: TISessionPrintRequest;
procedure StoreToPrinter(labelName: String; storeVariant: String);
property Author: String read Get_Author;
property DataSets: _GenericList read Get_DataSets;
property Description: String read Get_Description;
property Directory: String read Get_Directory;
property FileName: String read Get_FileName;
property FilePath: String read Get_FilePath;
property LabelObjects: TILabelObjectList read Get_LabelObjects;
property LabelSettings: TILabelSettings read Get_LabelSettings;
property PrintSettings: TIPrintSettings read Get_PrintSettings;
property Revision: Integer read Get_Revision;
property Title: String read Get_Title;
property Variables: TIVariableList read Get_Variables;
end;
...
{ TILabel }
constructor TILabel.Create(AObjVar: OleVariant);
begin
inherited Create(AObjVar);
end;
function TILabel.Clone: TILabel;
begin
Result := TILabel(TClrBaseObject.CreateInstance(InvokeMethod('Clone')));
end;
procedure TILabel.CreateLabelVariant(outputFileName: String; printTimeDataSourceNames: _StringArray);
begin
InvokeMethod('CreateLabelVariant', [S_CnClr_System_String, sC_Sys_StringArray], [outputFileName, printTimeDataSourceNames]);
end;
function TILabel.CreateXMLLabelInformation: _XDocument;
begin
Result := CoXDocument.Wrap(InvokeMethod('CreateXMLLabelInformation'));
end;
procedure TILabel.EndSessionPrint(sessionPrintRequest: TISessionPrintRequest);
begin
InvokeMethod('EndSessionPrint', [sC_NicSDK_ISessionPrintRequest], [GetDefaultIntf(sessionPrintRequest)]);
end;
function TILabel.GetLabelPreview(settings: TILabelPreviewSettings): Olevariant;
begin
Result := InvokeMethod('GetLabelPreview', [sC_NicSDK_ILabelPreviewSettings], [GetDefaultIntf(settings)]);
end;
function TILabel.Print(quantity: Integer): TIPrintRequest;
var
objVar: OleVariant;
begin
Result := nil;
objVar := InvokeMethod('Print', [S_CnClr_System_Int32], [quantity]);
if VarIsAssigned(objVar) then
Result := TIPrintRequest(TClrBaseObject.Create(objVar));
end;
function TILabel.PrintAll: TIPrintRequest;
begin
Result := TIPrintRequest(TClrBaseObject.CreateInstance(InvokeMethod('PrintAll')));
end;
function TILabel.PrintAllAsync: TIPrintRequest;
begin
Result := TIPrintRequest(TClrBaseObject.CreateInstance(InvokeMethod('PrintAllAsync')));
end;
function TILabel.PrintAsync(quantity: Integer): TIPrintRequest;
var
objVar: OleVariant;
begin
Result := nil;
objVar := InvokeMethod('PrintAsync', [S_CnClr_System_Int32], [quantity]);
if VarIsAssigned(objVar) then
Result := TIPrintRequest(TClrBaseObject.Create(objVar));
end;
function TILabel.PrintToGraphics(settings: TIPrintToGraphicsSettings): TIPrintToGraphicsResult;
var
objVar: OleVariant;
begin
Result := nil;
objVar := InvokeMethod('PrintToGraphics', [sC_NicSDK_IPrintToGraphicsSettings], [GetDefaultIntf(settings)]);
if VarIsAssigned(objVar) then
Result := TIPrintToGraphicsResult(TClrBaseObject.Create(objVar));
end;
procedure TILabel.SaveAs(fileName: String; overwrite: Boolean);
begin
InvokeMethod('SaveAs', [S_CnClr_System_String, S_CnClr_System_Boolean], [fileName, overwrite]);
end;
procedure TILabel.SessionPrint(quantity: Integer; sessionPrintRequest: TISessionPrintRequest);
begin
InvokeMethod('SessionPrint', [S_CnClr_System_Int32, sC_NicSDK_ISessionPrintRequest], [quantity, GetDefaultIntf(sessionPrintRequest)]);
end;
procedure TILabel.SetVariableValues(variablesList: String; ignoreMissingVariables: Boolean);
begin
InvokeMethod('SetVariableValues', [S_CnClr_System_String, S_CnClr_System_Boolean], [variablesList, ignoreMissingVariables]);
end;
procedure TILabel.SetVariableValuesXml(variablesList: String; ignoreMissingVariables: Boolean);
begin
InvokeMethod('SetVariableValuesXml', [S_CnClr_System_String, S_CnClr_System_Boolean], [variablesList, ignoreMissingVariables]);
end;
function TILabel.StartSessionPrint: TISessionPrintRequest;
begin
Result := TISessionPrintRequest(TClrBaseObject.CreateInstance(InvokeMethod('StartSessionPrint')));
end;
procedure TILabel.StoreToPrinter(labelName: String; storeVariant: String);
begin
InvokeMethod('StoreToPrinter', [S_CnClr_System_String, S_CnClr_System_String], [labelName, storeVariant]);
end;
function TILabel.Get_Author: String;
begin
Result := GetPropertyValue('Author');
end;
function TILabel.Get_DataSets: _GenericList;
begin
Result := CoGenericList.Wrap(GetPropertyValue('DataSets'));
end;
function TILabel.Get_Description: String;
begin
Result := GetPropertyValue('Description');
end;
function TILabel.Get_Directory: String;
begin
Result := GetPropertyValue('Directory');
end;
function TILabel.Get_FileName: String;
begin
Result := GetPropertyValue('FileName');
end;
function TILabel.Get_FilePath: String;
begin
Result := GetPropertyValue('FilePath');
end;
function TILabel.Get_LabelObjects: TILabelObjectList;
var
objVar: OleVariant;
begin
Result := nil;
objVar := GetPropertyValue('LabelObjects');
if VarIsAssigned(objVar) then
Result := TILabelObjectList(TClrBaseObject.Create(objVar));
end;
function TILabel.Get_LabelSettings: TILabelSettings;
var
objVar: OleVariant;
begin
Result := nil;
objVar := GetPropertyValue('LabelSettings');
if VarIsAssigned(objVar) then
Result := TILabelSettings(TClrBaseObject.Create(objVar));
end;
function TILabel.Get_PrintSettings: TIPrintSettings;
var
objVar: OleVariant;
begin
Result := nil;
objVar := GetPropertyValue('PrintSettings');
if VarIsAssigned(objVar) then
Result := TIPrintSettings(TClrBaseObject.Create(objVar));
end;
function TILabel.Get_Revision: Integer;
begin
Result := GetPropertyValue('Revision');
end;
function TILabel.Get_Title: String;
begin
Result := GetPropertyValue('Title');
end;
function TILabel.Get_Variables: TIVariableList;
var
objVar: OleVariant;
begin
Result := nil;
objVar := GetPropertyValue('Variables');
if VarIsAssigned(objVar) then
Result := TIVariableList(TClrBaseObject.Create(objVar));
end;
/ id
来过滤数组。
no
ES6与Set
进行查找。
var array1 = [{ id: 2, detail: 2 }, { id: 4, detail: 5 }],
array2 = [{ no: 1 }, { no: 4 }, { no: 7 }],
result = array1.filter(function (a) {
return array2.some(function (b) {
return a.id === b.no;
})
});
console.log(result);
答案 1 :(得分:0)
从这里开始:Displaying intersection of two arrays in angular js
function(arr1, arr2){
return arr1.filter(function(n) {
return arr2.indexOf(n) != -1
});
}
答案 2 :(得分:0)
let arr1 = [{ 'id': 2, 'detail': 2 },
{ 'id': 4, 'detail': 5 }];
let arr2 = [{ 'no': 1 },
{ 'no': 4, },
{ 'no': 7 }];
let result=arr1.filter(function(x){
for(let i in arr2){
if(arr2[i].no==x.id) return true
}
return false;
});
console.log(result);