简体   繁体   English

如何根据javascript中的条件获取arrayobject

[英]How to get the arrayobject based on condition in javascript

I would like to know how to find duplicates in fields and based on c我想知道如何在字段中查找重复项并基于 c

conditions get array object using javascript.条件使用 javascript 获取数组 object。

from arrayobj, get same property model ,then check从 arrayobj 获取相同的属性model ,然后检查

if typecode value is same but not SP , model same return that arrayobject如果 typecode 值相同但不是SP ,则 model 相同返回该数组对象

if typecode value has SP , model same return that arrayobject else return []如果 typecode 值有SP , model 同样返回 arrayobject else return []

for arrobj,对于 arrobj,

if model and typecode has same value, return those array object

if model same, any object typecode has value SP return those array

if above two conditions fails, return empty array
var arrobj1=[
   {id:1, model: "interior", name: "lisa", typecode: "FL"},
   {id:2, model: "interior", name: "peter", typecode: "FL"},
   {id:3, model: "exterior", name: "john", typecode: "SP"}
]
var result1=isValid(arrobj1);
Expected Output
// result1
[
  {id:1, model: "interior", name: "lisa", typecode: "FL"},
  {id:2, model: "interior", name: "peter", typecode: "FL"}
]


var arrobj2=[
   {id:1, model: "exterior", name: "lisa", typecode: "FL"},
   {id:2, model: "interior", name: "peter", typecode: "FP"},
   {id:3, model: "interior", name: "john", typecode: "SP"}
]
var result2=isValid(arrobj2);
Expected Output
// result2
[
  {id:2, model: "interior", name: "peter", typecode: "FP"},
   {id:3, model: "interior", name: "john", typecode: "SP"}
]

function isValid(list){
     let result=[];
    let duplicates = list.map((item, index) =>{
    return list.find((x, ind)=> x.model === item.model && index !== ind );
  })
   duplicates.forEach(e=>{
    if(e.typecode !== "SP"){
     result.push(e);
    }
  })
    return result;
}

Took a little time to understand your conditions,花了一点时间了解你的情况,

This code should do it, hope you understand it这段代码应该可以做到,希望你能理解

var arrobj1 = [
    { id: 1, model: "interior", name: "lisa", typecode: "FL" },
    { id: 2, model: "interior", name: "peter", typecode: "FL" },
    { id: 3, model: "exterior", name: "john", typecode: "SP" }
];

var arrobj2 = [
    { id: 1, model: "exterior", name: "lisa", typecode: "FL" },
    { id: 2, model: "interior", name: "peter", typecode: "FP" },
    { id: 3, model: "interior", name: "john", typecode: "SP" }
];

const isValid = (arr) => {
    const modelArrays = [];
    const modelArraysCount = {};

    const typecodeArrays = [];
    const typecodeArraysCount = {};

    arr.forEach(item => {
        modelArrays.push(item.model)
        typecodeArrays.push(item.typecode)
    });

    modelArrays.forEach(model => {
        if (modelArraysCount[model]) {
            modelArraysCount[model] += 1;
        } else {
            modelArraysCount[model] = 1;
        }
    });

    typecodeArrays.forEach(typecode => {
        if (typecodeArraysCount[typecode]) {
            typecodeArraysCount[typecode] += 1;
        } else {
            typecodeArraysCount[typecode] = 1;
        }
    });
    
    // console.log(modelArraysCount)
    // console.log(typecodeArraysCount)
    // console.log(modelArraysCount[0])

    var modelResult;
    for (const key in modelArraysCount) {
        if (modelArraysCount[key] === Math.max(...Object.values(modelArraysCount))) {
            modelResult = key;
            break;
        }
    }

    var typecodeResult;
    for (const key in typecodeArraysCount) {
        if (typecodeArraysCount[key] === Math.max(...Object.values(typecodeArraysCount))) {
            typecodeResult = key;
            break;
        }
    }

    // console.log(modelArraysCount, typecodeArraysCount)

    return arr.filter(i => {
        return (i.model == modelResult && i.typecode == typecodeResult) || (i.model == modelResult);
    })
}

console.log(isValid(arrobj2))

And I didn't understand your second condition about the model being the same and SP and all but tried to fulfill your required output.我不明白你关于 model 相同和 SP 的第二个条件,但几乎试图满足你要求的 output。

Please do let me know if there is any confusion.如果有任何混淆,请告诉我。 Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM