Deleting the exact object in array when value is false - javascript

My code looks like this:
var data = someDataWhichComesFromOtherMethod
var array = [
{name: "one", value:"data1", caption:"aaa"...},
{name: "two", value:"data2", caption:"bbb"...},
{name: "three", value:"data3", caption:"ccc"...},
{name: "four", value:"data4", caption:"ddd"...}
...
]
What I want to do is to: first check if the array is not empty and later check if incoming data is true or not and when it's not, whole object needs to be deleted from the array.
I started with:
if (array && array.length) {
//true
} else {
//false
}
For example, if data4 is null or "", array should look like this:
var array = [
{name: "one", value:"data1", caption:"aaa"...},
{name: "two", value:"data2", caption:"bbb"...},
{name: "three", value:"data3", caption:"ccc"...}
...
]

If you are trying to test the value of value and remove the item from the array when it is falsey, you could use filter to get the items that are truthy.
var array = [
{name: "one", value:"data1", caption:"aaa"},
{name: "two", value:"data2", caption:"bbb"},
{name: "three", value:"data3", caption:"ccc"},
{name: "four", value:null, caption:"ddd"},
{name: "five", value:"", caption:"eee"}
]
var result = array.filter(item => item.value)
console.log(result);

Related

How to check two array of objects property using javascript [duplicate]

This question already has answers here:
Comparing Arrays of Objects in JavaScript
(18 answers)
Closed 10 months ago.
I would like to know how to return true or false based on two array objects
property using javascript
If arrobj1 and arrobj2 value and country same return true
I have tried below, may i know better way to do using javascript
Tried
for(var elem of arrobj1){
const result = arrobj2.filter(obj => obj.value === elem.value && obj.country === elem.country);
if(result.length > 0){
return true;
}
}
var arrobj1 =[
{id:1, name: "one", value:"sales", country:"MY"},
{id:2, name: "two", value:"finance", country:"PH"},
{id:3, name: "three", value:"digital", country:"SL"}
]
var arrobj2 =[
{id:4, name: "four", value:"digital", country:"SL"}
]
Expected Output
true
Rearrange your code to start with the arrays then the sorting function
arrobj2 should be:
var arrobj2 =[
{id:4, name: "four", value:"digital", country:"SL"}
]
instead of:
var arrobj2 =[{
{id:4, name: "four", value:"digital", country:"SL"}
}]
also try console.log('true') instead if return true. You'll notice something that would help you later
Your code works perfectly. To use the return keyword, you have to wrap your code in a function and call it when needed.
Try this
var arrobj1 = [
{id:1, name: "one", value:"sales", country:"MY"},
{id:2, name: "two", value:"finance", country:"PH"},
{id:3, name: "three", value:"digital", country:"SL"}
]
var arrobj2 = [
{id:4, name: "four", value:"digital", country:"SL"}
];
function exist(arrobj1, arrobj2){
for(var elem of arrobj1){
const result = arrobj2.filter(obj => obj.value === elem.value && obj.country === elem.country);
if(result.length > 0){
return true;
}
}
return false;
}
console.log(exist(arrobj1, arrobj2));
You can use Array#some as follows:
const
arrobj1 =[ {id:1, name: "one", value:"sales", country:"MY"}, {id:2, name: "two", value:"finance", country:"PH"}, {id:3, name: "three", value:"digital", country:"SL"} ],
arrobj2 =[ {id:4, name: "four", value:"digital", country:"SL"} ],
found = arrobj2.some(
({value:v,country:c}) =>
arrobj1.map(
({value,country}) => `${value}|${country}`
).includes(`${v}|${c}`)
);
console.log( found );

how to add an object with two pairs to an array that has a key and values that are same as the object

I need to build an array based on server data. I get the data as an object like this:
{name: "test", hobby: "test"}
and my array that I want to add this object to looks like this:
[0: {name: "test1", hobby: "test1"}, 1 : {name: "test2", hobby: "test2"}]
output should be:
[0: {name: "test1", hobby: "test1"}, 1 : {name: "test2", hobby: "test2"}, 2 : {name: "test", hobby: "test"}]
How do I add the element to the array? Push did not work in this case. I need to add the key to the element and then add it to the end of array but I don't know how.
Please let me know what are the options.Thanks.
if you want to add key, you should use object.
like
let obj = {};
function addData(){
let length = Object.keys(obj).length
temp = {name:"test"+(length+1),hobby:"test"+(length+1)}
obj[length]= temp;
console.log(obj);
}

Needs to group by multi selected value array

This my array & i needs to group by dept. Here dept is an array so i need to group by its dept value.
myArray = [
{name: "one", dept: ["red", "blue"]},
{name: "two", dept: ["green", "blue"]}
]
I tried with this code but i can't get the what i need.
let deptObj = {};
_.map(myArray , el => {
const mySubArray= [];
_.filter(el.dept, (depts, i) => {
mySubArray.push(el.member);
deptObj[depts] = mySubArray;
})
}
});
I need result like this, anybody can help me?
deptObj={
red:[
{name: "one", dept:["red", "blue"]}
],
blue:[
{name: "one", dept:["red", "blue"]},
{name: "two", dept:["green", "blue"]}
],
green:[
{name: "two", dept:["green", "blue"]}
]
}
This solution does what you need:
let myArray = [
{ name: "one", dept: ["red", "blue"] },
{ name: "two", dept: ["green", "blue"] }
];
let deptObj = {};
myArray.forEach(item => {
item.dept.forEach(dept => {
if (!deptObj[dept]) {
deptObj[dept] = [item];
} else {
deptObj[dept].push(item);
}
});
});
console.log(deptObj);
You can use Array.reduce():
var arr = [
{name: "one", dept: ["red", "blue"]},
{name: "two", dept: ["green", "blue"]}
];
var result = arr.reduce((a,curr)=>{
curr.dept.forEach((e)=>{ (a[e] = (a[e] || [])).push(curr)});
return a;
},{});
console.log(JSON.parse(JSON.stringify(result)));
Interesting problem. Here is a another solution which is "one liner" and uses .reduce/.map/_.concat:
const arr = [{name: "one", dept: ["red", "blue"]},{name: "two", dept: ["green", "blue"]}]
var result = arr.reduce((r, {dept}, i, a) => dept.map(x => r[x] = _.concat(r[x] || [], [a[i]])) && r, {})
console.log(result)

Search for all values in array of objects

I have three arrays.
One of them contains values I will be testing. The two others are arrays of object which might include the values of my first array under the name key.
const myArray = ["foo", "bar"];
const testArray1 = [
{name: "foo"},
{name: "bar"},
{name: "something else"}
]
const testArray2 = [
{name: "foo"},
{name: "rab"},
{name: "something else"}
]
I am trying to write a condition which would return true only if the tested array contains all of the values of my first array.
With the same example it would give me something like this :
if (testArray1.containsAll(myArray)) // true
if (testArray2.containsAll(myArray)) // false
What is the best way to resolve this ?
Thanks, any help much appreciated
With array.prototype.every and array.prototype.find, it should be:
const myArray = ["foo", "bar"];
const testArray1 = [
{name: "foo"},
{name: "bar"},
{name: "something else"}
];
const testArray2 = [
{name: "foo"},
{name: "rab"},
{name: "something"}
];
console.log(myArray.every(s => testArray1.find(o => o.name === s)));
console.log(myArray.every(s => testArray2.find(o => o.name === s)));
can be use, every and some. these are return only true/false
const myArray = ["foo", "bar"];
const testArray1 = [
{name: "foo"},
{name: "bar"},
{name: "something else"}
]
const testArray2 = [
{name: "foo"},
{name: "rab"},
{name: "something else"}
]
let result1 = testArray1.every(item => myArray.some(array => item.name == array))
let result2 = testArray2.every(item => myArray.some(array => item.name == array))
console.log('result1', result1)
console.log('result2', result2)
Check this out. May not be the best way but works perfectly fine.
const myArray = ["foo", "bar"];
const testArray1 = [
{name: "foo"},
{name: "bar"},
{name: "something else"}
]
const testArray2 = [
{name: "foo"},
{name: "rab"},
{name: "something else"}
]
let aFlag = testArray1.filter( a => myArray.includes(a.name)).length === myArray.length;
let bFlag = testArray2.filter( a => myArray.includes(a.name)).length === myArray.length;
console.log(aFlag, bFlag)

How to group an array of objects individually into single ones

I'm using lodash to group an array of objects:
let people = [
{name:'carl',group:'one'}, 
{name:'john',group:'one'}, 
{name:'dean',group:'three'}
]
_.groupBy(people,"group");
Result:
one: Array (2 items)
0: Object {group: "one", name: "carl"}
1: Object {group: "one", name: "john"}
three: Array (1 item)
0: Object {group: "three", name: "dean"}
What I want is the objects with group array length higher than 1 to separate into another array individually like this:
one: Array (1 item)
0: Object {group: "one", name: "carl"}
one: Array (1 item)
0: Object {group: "one", name: "john"}
three: Array (1 item)
0: Object {group: "three", name: "dean"}
What is the best way to do that?
I think you can use the method "sort"
let people = [{name:'carl',group:'one'},
{name:'john',group:'one'},
{name:'dean',group:'three'}];
people.sort(function(a,b){
return a.name.localeCompare(b.name)
});
Reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Categories