'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
conditions get array object using javascript.
from arrayobj, get same property model,then check
if typecode value is same but not SP, model same return that arrayobject
if typecode value has SP, model same return that arrayobject
else return []
for 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;
}
Solution 1:[1]
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.
Please do let me know if there is any confusion. Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Azwad Jahan |
