Comparing a object with a test object - javascript

How can I compare the object with the test object in Javascript. And if the Testobject sets the same value to true output this Object Item into a new Object.
Obj1 = [{
name: Hugo,
itsTrustee: false,
itsSchollar: true,
},
{
name:Bugo,
itsTrustee: true,
itsSchollar: false,
}];
testObj = {
itsTrustee: true,
itsSchollar: false,
}
I tried Reduce and map. But I can't get the right value
Like that:
this.obj1.map((item) => {
let newObj = Object.keys(item).reduce((acc, x) => {
});
});
But since nothing works correctly, I cannot show an example function here

first you can restructure a little like this
Obj1 = [{
name: "Hugo",
something: {
itsTrustee: false,
itsSchollar: true
}
},
{
name: "Hugo",
something: {
itsTrustee: false,
itsSchollar: true
}
}
];
then you can try using find function here is an example
const data = [20, 18, 15, 10, 9];
let found = data.find(element => element < 12);
console.log(found);
for you it's going to be something like
let found = data.find(element => JSON.stringify(element.something) == JSON.stringify(testObj));
if found is null that's mean there's no object with your
wish it helped you

Related

How to sort array of objects with boolean values: true, false and null

Hi i have an array of objects that i want to sort based on a boolean that one of the objects has. However normally there would be either true or false but in this case we also check on null values because sometimes the data has not been set and in that case we wanna show that it has yet to be set with an icon.
Here's an example of the array:
const arrayOfObjects = [
{
id: 69,
boolean: true,
name: 'foo',
},
{
id: 42,
boolean: false,
name: 'bar',
},
{
id: 666,
boolean: null,
name: 'foo',
},
{
id: 420,
boolean: false,
name: 'bar',
},
{
id: 2,
boolean: null,
name: 'foo',
},
{
id: 123,
boolean: true,
name: 'foo',
},
]
So what i tried first was:
arrayOfObjects.sort((a, b) => b.boolean - a.boolean);
This sets the objects that are true at the front but the objects with false or null are scattered.
Then i tried:
arrayOfObjects.sort((a, b, c) => (c.boolean - b.boolean) - a.boolean);
This just didn't work at all.
I couldn't really find a case that was similar enough to base a solution off of it so hopefully i can find it here.
If you like to use a custom sorting, you could take an object with the wanted sorting, like
const
order = { true: 1, null: 2, false: 3 };
data = [{ id: 69, boolean: true, name: 'foo' }, { id: 42, boolean: false, name: 'bar' }, { id: 666, boolean: null, name: 'foo' }, { id: 420, boolean: false, name: 'bar' }, { id: 2, boolean: null, name: 'foo' }, { id: 123, boolean: true, name: 'foo' }];
data.sort((a, b) => order[a.boolean] - order[b.boolean]);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you have unknown values and want to move them to bottom, you could add another key with a large value, like
order = { true: 1, null: 2, false: 3, bottom: Number.MAX_VALUE };
Usage:
data.sort((a, b) =>
(order[a.boolean] || order.bottom) - (order[b.boolean] || order.bottom)
);
You can check for the null explicitly ...
let list = [{i: 0, boolean: true}, { i: 1, boolean: null}, { i:2, boolean: false}, { i: 4, boolean: true}]
function cpBoolWithNull(a,b) {
//if both are null return 0 to maintain a stable sort
//if only one is null return 0 or 1 depending on the value of the other
if (a.boolean === null) return b.boolean === null ? 0 : b.boolean ? 1 : -1;
if (b.boolean === null) return a.boolean ? -1 : 1;
//if both are different from null, sort true before false
return b.boolean - a.boolean
}
console.log(list.sort(cpBoolWithNull));
This will sort true ... null ... false If you need a differnt order, adjust the return values.
I think that you can have a type checker with JS with this simple script.
let array =[true, false, null];
function check(i){
if (array[i] != null||array[i]!=false){
if (array[i]!=null || array[i]!=true)document.write(" Array item"+" "+i+" "+"has the value of boolean false. ");
if (array[i]!=true||array[i]!=false)document.write(" Array item"+" "+i+" "+"has the value of boolean true. ");
if (array[i] != true || array[i] != false )document.write(" Array item"+" "+i+" "+"has the value of object null. ");
document.write("<br>")
}
}
check(0);
You can comment out the other text when it is not needed.

Compare Nested Objects and save difference in new object using Javascript

I have two Javascript objects
var order1 = {
sandwich: 'tuna',
chips: true,
drink: 'soda',
order: 1,
toppings: [{VendorNumber: 18, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 19, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 20, PreferredFlag: false, SupportedFlag: true}],
details: {
name: 'Chris',
phone: '555-555-5555',
email: 'no#thankyou.com'
},
otherVal1: '1'
};
var order2 = {
sandwich: 'turkey',
chips: true,
drink: 'soda',
order: 2,
toppings: [{VendorNumber: 18, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 19, PreferredFlag: false, SupportedFlag: false}, {VendorNumber: 20, PreferredFlag: true, SupportedFlag: true}],
details: {
name: 'Jon',
phone: '(555) 555-5555',
email: 'yes#please.com'
},
otherVal1: '2'
};
What I need is to compare these two objects (order1 is existing and order2 is the edited data) and store the difference in a new variable named var order3. However if there is an array inside an object like the toppings array to be copied as whole with the changes.
In short the result should be
{
details: {
email: "yes#please.com",
name: "Jon",
phone: "(555) 555-5555"
},
order: 2,
otherVal1: "2",
sandwich: "turkey",
toppings: [{
PreferredFlag: false,
SupportedFlag: true,
VendorNumber: 18
}, {
PreferredFlag: false,
SupportedFlag: false,
VendorNumber: 19
}, {
PreferredFlag: true,
SupportedFlag: true,
VendorNumber: 20
}]
}
How can i achieve this ?
This gives you exactly what you wanted:
function diff(tgt, src) {
if (Array.isArray(tgt)) { // if you got array
return tgt; // just copy it
}
// if you got object
var rst = {};
for (var k in tgt) { // visit all fields
if (typeof src[k] === "object") { // if field contains object (or array because arrays are objects too)
rst[k] = diff(tgt[k], src[k]); // diff the contents
} else if (src[k] !== tgt[k]) { // if field is not an object and has changed
rst[k] = tgt[k]; // use new value
}
// otherwise just skip it
}
return rst;
}
console.log(diff(order2, order1));
I think you are looking for a diff'ing algorithm. Wrote this quick recursive function that iterates over each enumerable property of your JavaScript object (not json object) testing equality. Note that the position of arguments does affect the output
function diff(obj1, obj2) {
if (typeof obj1 === "object") {
const obj = {};
for (const prop in obj1) {
if (diff(obj1[prop], obj2[prop])) {
obj[prop] = obj1[prop]
}
}
return obj
} else {
return obj1 !== obj2;
}
}
console.log(diff(order2, order1))

How to create/merge object from splitted string array in TypeScript?

I have an array of objects like below;
const arr1 = [
{"name": "System.Level" },
{"name": "System.Status" },
{"name": "System.Status:*" },
{"name": "System.Status:Rejected" },
{"name": "System.Status:Updated" }
]
I am trying to split name property and create an object. At the end I would like to create an object like;
{
"System.Level": true,
"System.Status": {
"*": true,
"Rejected": true,
"Updated": true
}
}
What I have done so far;
transform(element){
const transformed = element.split(/:/).reduce((previousValue, currentValue) => {
previousValue[currentValue] = true;
}, {});
console.log(transofrmed);
}
const transofrmed = arr1.foreEach(element => this.transform(element));
The output is;
{System.Level: true}
{System.Status: true}
{System.Status: true, *: true}
{System.Status: true, Rejected: true}
{System.Status: true, Updated: true}
It is close what I want to do but I should merge and give a key. How can I give first value as key in reduce method? Is it possible to merge objects have same key?
You could reduce the splitted keys adn check if the last level is reached, then assign true, otherwise take an existent object or a new one.
const
array = [{ name: "System.Level" }, { name: "System.Status" }, { name: "System.Status:*" }, { name: "System.Status:Rejected" }, { name: "System.Status:Updated" }],
object = array.reduce((r, { name }) => {
var path = name.split(':');
last = path.pop();
path.reduce((o, k) => o[k] = typeof o[k] === 'object' ? o[k] : {}, r)[last] = true;
return r;
}, {});
console.log(object);
Use Array.reduce() on the list of properties. After splitting the path by :, check if there is second part. If there is a second part assign an object. Use object spread on the previous values, because undefined or true values would be ignored, while object properties would be added. If there isn't a second part, assign true as value:
const array = [{ name: "System.Level" }, { name: "System.Status" }, { name: "System.Status:*" }, { name: "System.Status:Rejected" }, { name: "System.Status:Updated" }];
const createObject = (arr) =>
arr.reduce((r, { name }) => {
const [first, second] = name.split(':');
r[first] = second ? { ...r[first], [second]: true } : true;
return r;
}, {});
console.log(createObject(array));

How to loop through an array containing objects of objects

This is my array:
[{
name: "Test",
skills: {
agile: true,
good: true
}
},
{
name: "Test 2",
skills: {
agile: false,
good: false
}
}]
I need to find the last element (his index) who has the skill good set to true. The only way I know to fix this is to use a for/if combination. Is there any other faster/optimal way to do it ?
Use filter:
const goodSkills = myArray.filter(x => x.skills.good)
Then get the last item:
goodSkills[goodSkills.length - 1]
Or if you only need the index, and we treat name as a unique key:
const lastGoodIndex = myArray.findIndex(x => x.name === goodSkills[goodSkills.length - 1].name)
You can then use lastGoodIndex for whatever nefarious purpose you have in mind.
Alternatively if name is not a unique key, I suggest just using forEach:
let lastGoodIndex;
myArray.forEach((x, i) => lastGoodIndex = x.skills.good ? i : lastGoodIndex);
console.log(lastGoodIndex);
The fastest way is to us a for loop:
var arr = [{
name: "Test",
skills: {
agile: true,
good: true
}
},
{
name: "Test 2",
skills: {
agile: false,
good: false
},
}]
function findLastGoodIndex(arr) {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i].skills.good) {
return i;
}
}
}
console.log(findLastGoodIndex(arr));
Or if the list isn't that large you can combine reverse with findIndex:
arr.reverse().findIndex(x => x.skills.good));
You can do it in 3 rows:
var arr = [
{
name: "Test",
skills: {
agile: true,
good: true
}
},
{
name: "Test 2",
skills: {
agile: false,
good: false
},
}
]
var lastIndex;
arr.forEach(function(item, i) {
if(item.skills.good) lastIndex = i;
})
console.log(lastIndex);
If you want the index of last element that has good = true your best option is to use for/if (perhaps going backwards, i.e. i-- if you can guess where can you expect the good item?).
filter will find the item(s) too, and it also has index property, but generally it's slower than for/if.
forEach will also generally be slower than for/if.
edit:styling.
Since you need to search the last value, you should search in descending order. This would prevent any unnecessary iterations.
var data = [{
name: "Test",
skills: {
agile: true,
good: true
}
},
{
name: "Test 2",
skills: {
agile: false,
good: false
}
}
];
var i = data.length;
var result = null;
while (i--) {
if (data[i].skills.good) {
result = i;
break;
}
}
console.log(result);

return non array in using map

I used map to loop but it returned an array, not sure I should use something else like forEach. I have this initial object.
data.discounts: [{
days: 3,
is_enable: true
},{
days: 10,
is_enable: false
}]
Then I do the checking on is_enable
const newObj = {
"disableDiscount_3": !isEmpty(data.discounts) ? (data.discounts.map(obj => obj.days === 3 && obj.is_enable === true ? true : false)) : ''
}
then it became
newObj.disableDiscount_3 = [{
true,
false,
false,
false
}]
What I want is actually just true or false like: newObj.disableDiscount_3 = true What should I do?
map() method is not meant to be used for that, instead you can use some() that will check if specified object exists and return true/false.
var discounts = [{
days: 3,
is_enable: true
}, {
days: 10,
is_enable: false
}]
var check = discounts.some(e => e.days == 3 && e.is_enable === true);
console.log(check)
To first find specific object you can use find() method and if the object is found then you can take some property.
var data = {
discounts: [{
days: 3,
is_enable: true,
value: 123
}, {
days: 10,
is_enable: false
}]
}
var obj = {
"discount_3": (function() {
var check = data.discounts.find(e => e.days == 3 && e.is_enable === true)
return check ? check.value : ''
})()
}
console.log(obj)

Categories

Resources