Javascript 比较两个数组值,如果不匹配则填充
我需要帮助匹配两个数组。我在示例代码中使用了TypeScript,但它可以理解,因为它或多或少地涉及数组操作 简而言之:Javascript 比较两个数组值,如果不匹配则填充,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我需要帮助匹配两个数组。我在示例代码中使用了TypeScript,但它可以理解,因为它或多或少地涉及数组操作 简而言之: public async getUserWidgets(): Promise<Widget[]> { let allWidgets = await this.getAllWidgets(); //Array with all the links ID from the list let userRepository = new User
public async getUserWidgets(): Promise<Widget[]> {
let allWidgets = await this.getAllWidgets(); //Array with all the links ID from the list
let userRepository = new UserProfileRepository(this.absoluteWebUrl);
let userSettings = await
userRepository.getUserExtensionValues(this.context); //Extracting the user Settings which contains the ID of the saved linksvar
result:Widget[] = []; //the array where the result will go in
//if the user doesnt have any saved links, or if the user have less than 4 saved links
if (userSettings == null || userSettings.QuickLinksWidgets == null ||
userSettings.QuickLinksWidgets.length < 4)
{result = allWidgets.filter((w) => {return w.defaultWidget;}).slice(0,4);
}
else {var ids =userSettings.QuickLinksWidgets;
for (let i = 0; i < 4; i++) {
let id = '' + ids[i];let w = allWidgets.filter((e) => { return e.id == id;});
if (w.length == 0) {
continue;}
result.push(w[0]);}}
return new Promise<Widget[]>(async (resolve) => {resolve(result);});}
我有两个数组<代码>我的项目[]和所有项目[]
<代码>我的项目[]最多只能保存4个值
我想首先检查myItems[]
中的项目是否为4,和/或是否存在于其他数组allItems[]
中
如果没有:用allItems[]
中的值填充myItems[]
(直到它包含4个值),和/或用allItems[]
中的其他项替换缺少的项(相对于allItems[])(我尝试使用默认值,而不是在示例代码中随机取值)
说明:
public async getUserWidgets(): Promise<Widget[]> {
let allWidgets = await this.getAllWidgets(); //Array with all the links ID from the list
let userRepository = new UserProfileRepository(this.absoluteWebUrl);
let userSettings = await
userRepository.getUserExtensionValues(this.context); //Extracting the user Settings which contains the ID of the saved linksvar
result:Widget[] = []; //the array where the result will go in
//if the user doesnt have any saved links, or if the user have less than 4 saved links
if (userSettings == null || userSettings.QuickLinksWidgets == null ||
userSettings.QuickLinksWidgets.length < 4)
{result = allWidgets.filter((w) => {return w.defaultWidget;}).slice(0,4);
}
else {var ids =userSettings.QuickLinksWidgets;
for (let i = 0; i < 4; i++) {
let id = '' + ids[i];let w = allWidgets.filter((e) => { return e.id == id;});
if (w.length == 0) {
continue;}
result.push(w[0]);}}
return new Promise<Widget[]>(async (resolve) => {resolve(result);});}
我有一个小部件(快速链接)模块,一次显示4个链接,但总共有20个不同的链接(或更多)。所有链接都存储在一个列表中,每个链接都有自己的唯一ID。在代码中,所有链接都被提取并返回到一个数组中(如上面示例中的allItems[])
用户可以保存他/她希望在小部件中显示的链接。用户设置以数组的形式存储并返回,其中包含用户保存的链接的ID。像上面的myItems[]
一样
问题:
public async getUserWidgets(): Promise<Widget[]> {
let allWidgets = await this.getAllWidgets(); //Array with all the links ID from the list
let userRepository = new UserProfileRepository(this.absoluteWebUrl);
let userSettings = await
userRepository.getUserExtensionValues(this.context); //Extracting the user Settings which contains the ID of the saved linksvar
result:Widget[] = []; //the array where the result will go in
//if the user doesnt have any saved links, or if the user have less than 4 saved links
if (userSettings == null || userSettings.QuickLinksWidgets == null ||
userSettings.QuickLinksWidgets.length < 4)
{result = allWidgets.filter((w) => {return w.defaultWidget;}).slice(0,4);
}
else {var ids =userSettings.QuickLinksWidgets;
for (let i = 0; i < 4; i++) {
let id = '' + ids[i];let w = allWidgets.filter((e) => { return e.id == id;});
if (w.length == 0) {
continue;}
result.push(w[0]);}}
return new Promise<Widget[]>(async (resolve) => {resolve(result);});}
我有一个解决方案,可以检查myItems[]
的长度,如果需要,可以从allItems[]
中填充项目。但是,它不会检查用户数组中的项是否存在于allItems[]
中,然后用默认链接填充它。在实践中,这意味着用户可以保存链接,并按预期显示在小部件中。但是如果在列表中删除了链接(然后将在allItems
数组中删除该链接),则只有3项将显示为myItems[]
不会检查allItems[]
数组中是否存在该链接
代码:
public async getUserWidgets(): Promise<Widget[]> {
let allWidgets = await this.getAllWidgets(); //Array with all the links ID from the list
let userRepository = new UserProfileRepository(this.absoluteWebUrl);
let userSettings = await
userRepository.getUserExtensionValues(this.context); //Extracting the user Settings which contains the ID of the saved linksvar
result:Widget[] = []; //the array where the result will go in
//if the user doesnt have any saved links, or if the user have less than 4 saved links
if (userSettings == null || userSettings.QuickLinksWidgets == null ||
userSettings.QuickLinksWidgets.length < 4)
{result = allWidgets.filter((w) => {return w.defaultWidget;}).slice(0,4);
}
else {var ids =userSettings.QuickLinksWidgets;
for (let i = 0; i < 4; i++) {
let id = '' + ids[i];let w = allWidgets.filter((e) => { return e.id == id;});
if (w.length == 0) {
continue;}
result.push(w[0]);}}
return new Promise<Widget[]>(async (resolve) => {resolve(result);});}
公共异步getUserWidgets():承诺{
让allWidgets=等待。getAllWidgets();//包含列表中所有链接ID的数组
让userRepository=newuserprofilerepository(this.absoluteWebUrl);
让userSettings=wait
userRepository.getUserExtensionValues(this.context);//提取包含已保存链接的ID的用户设置
结果:小部件[]=[];//结果将进入的数组
//如果用户没有任何保存的链接,或者如果用户保存的链接少于4个
如果(userSettings==null | | userSettings.QuickLinksWidgets==null | |
userSettings.QuickLinksWidgets.length<4)
{result=allWidgets.filter((w)=>{return w.defaultWidget;}).slice(0,4);
}
else{var id=userSettings.QuickLinksWidgets;
for(设i=0;i<4;i++){
let id=''+id[i];let w=allWidgets.filter((e)=>{returne e.id==id;});
如果(w.length==0){
继续;}
result.push(w[0]);}
返回新承诺(异步(解析)=>{resolve(result);});}
检查数组是否包含值的一种简单方法是使用
includes()
方法
for(let value of myitems){
if(allitems.includes(value)){
console.log("Duplicate")
}
}
上述代码将循环遍历myitems
数组中的每个值,并测试该值是否在allitems
数组中