Comparing collection keys in RxJS? - javascript

Wanted to see if RxJS has a quick / elegant way of doing this.
Suppose we have two different array of objects. For example:
A1:
[{ name: 'Sue', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 25 },
{ name: 'Sarah', age: 35 }]
A2:
[{ name: 'Sue', age: 25 },
{ name: 'Frank', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Sarah', age: 35 }]
The keys we want to compare are identified by the name property.
I was thinking about just producing two arrays of all the names filtering out the duplicates, and then comparing them to make sure they are equal, but thought perhaps RxJS has a slick way of doing this and could also emit an observable of any names that don't have a match?

You can try merging the two lists and then applying the distinct operator with a key selector function (to specify key is "name") so that you get a stream of the de-duped items.
https://rxjs-dev.firebaseapp.com/api/operators/distinct

Related

DynamoDB, BatchWriteItem for an array - AWS V3

I have an array I am using to create a put for batchwriteitem.
const people = [{
location: 'London',
name: 'Tony Wilson',
pets: [ {name: 'Cuddles', age: 4}, { name: 'Jess', age: 2}]
},
{
location: 'Manchester',
name: 'Liz Smith',
pets: [ {name: 'Fluffy', age: 4}, { name: 'Keith the cat', age: 2}]
}
]
My batchwriteitem loop is working for individual items
location: { S : item.location },
but when I try and input the pets array as an array it fails
pets: { M: item.pets },
So this all works except the pets array.
const request = pets.map(item => {
const createdDate = Date.now();
return {
PutRequest: {
Item: {
location: { S : item.location },
createdDate:{ N: createdDate },
pets: { M: item.pets }
}
}
});
Do I need to break down the pets array into types and if so how can I achieve this?
Edit
pets: { L: item.pets }, does not work
**Cannot read properties of undefined (reading '0')
and the old syntax without the types does not work on v3 with the document client for me.
Pets is an array, which is also known as a list type. You are setting it as a dictionary/map type as you've set the value to M. It should be L:
pets: { L: item.pets },
I would advise that you use the Document Client as it means you do not need to think about the type conversions, and just use native JSON objects:
pets: item.pets,

Creating An Array of Objects From An Object Without Specific Key Values

Hello this is my first question on this platform so please feel free to remove or edit, or leave criticism.
So I am parsing data from an API in react. The data comes out looking something like this.
data = {
"20": "john",
"20.5": "jim",
"21": "bob",
"21.5": "james",
"22": "carl",
"23": "linda",
"25": "jack",
"25.5": "kyla",
"26": "jenny",
}
And I am trying to dynamically update a webpage using this data. I need to access the ages (keys) and the names (values).
I am putting the object's key value pairs into an array so I can map them inline using array.map(), my code below.
var Array = []
for (const [key, value] of Object.entries(data)) {
Array.push({
age: key,
name: value,
});
}
Now this does for the most part what I am trying to do. The output being a new array.
[
{ age: '20', name: 'john' },
{ age: '21', name: 'bob' },
{ age: '22', name: 'carl' },
{ age: '23', name: 'linda' },
{ age: '25', name: 'jack' },
{ age: '26', name: 'jenny' },
{ age: '20.5', name: 'jim' },
{ age: '21.5', name: 'james' },
{ age: '25.5', name: 'kyla' }
]
Now the problem is that they are not in the same order as they were given. For some reason the .5 ages all sorted at the end of the array. Is this a limitation of array.push()? Am I doing something fundamentally wrong with the Object.entries() function? Or should I simply resort to using sorting methods once the array has been created?
When iterating over objects, ascending whole number keys will always come first. The best approach to this sort of problem would be to change the backend so that it gives you the desired array of objects to begin with, instead of a somewhat broken format that you need to fix later.
If that's not possible, you'll have to fix it client-side by taking all entries, then sorting them.
const input = {
20: 'john',
20.5: 'jim',
21: 'bob',
21.5: 'james',
22: 'carl',
23: 'linda',
25: 'jack',
25.5: 'kyla',
26: 'jenny'
};
const result = Object.entries(input)
.map(([age, name]) => ({ age, name }))
.sort((a, b) => a.age - b.age);
console.log(result);
You may also need to fix the API so that it gives you proper JSON, since
{
20: john
20.5: jim
is not valid syntax.

Convert array of objects to another array of objects

I need to convert array of objects to just objects to use it in table datasource:
{v_report_full: Array(25)}
v_report_full: Array(25)
0:
first_name: "Blake"
last_name: "Thorphan"
middle_name: "Agder"
monh: "2021-02-01 00:00:00"
n_vh: "Delay 00:04:52"
n_vh_s_obeda: "ok"
n_vyh: "ok"
n_vyh_s_obeda: "ok"
name: "oitib"
rtime: null
vh_s_obeda_ts: null
vhod_ts: "2021-02-01 08:59:52"
vyh_s_obeda_ts: null
vyhod_ts: null
__typename: "Ereport"
__proto__: Object
1:.. the same next index
2:.. the same next index
Table datasource requires next order:
const dataSource = [
{
key: '1',
name: 'Mike',
age: 32,
address: '10 Downing Street',
},
{
key: '2',
name: 'John',
age: 42,
address: '10 Downing Street',
},
];
I've tried to use Object.keys, but have not enough expirience to do that. If here is a simple solution, if not I have to write a loop for each row with function to concat, etc?
Upd. These one was I worked with:
let dataSource = (data) ? data.v_report_full.map((value, index, array) => {
return {
id: value.id,
key: value.id,
first_name:(value, 'first_name'),
.....
}
After hatfield advise, I checked once more and it's working! A liitle changes and above code then working now D: What a noob I am.

Need to add a string to the end of first names in an array using lodash

I have the following array.
var gillFamily = [
{ name: 'john', age: 20 },
{ name: 'richard', age: 27 },
{ name: 'debbie', age: 55 },
{ name: 'dan', age: 25 },
{ name: 'robin', age: 60 }
];
I need to print the names with the lastname "Gill" added to them using lodash.
I've tried this which was the closest I got:
_.map(gillFamily, "name") + " Gill";
but that only adds Gill to the last name in the array.
How do I add the name to all items in the array?
One option is:
_.map(gillFamily, (el) => el.name + " Gill");
In case that the environment doesn't support ES6:
_.map(gillFamily, function(el) {
return el.name + " Gill"
});
You could also use Array.prototype.map function.
You need to access the name property inside the Object and then add the Gill string to that name.
var gillFamily = [ {name: 'john', age: 20},
{name: 'richard', age: 27},
{name: 'debbie', age: 55},
{name: 'dan', age :25},
{name: 'robin', age : 60}];
var gillFamilyWithLastNameAdded = _.map(gillFamily, person => person.name + " Gill");
console.log(gillFamilyWithLastNameAdded);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Using the map function you can map each element with the last name appended to using the following. Using _.each(), you can loop over the array of objects and update the property/properties desired. This probably may not be the best/most efficient technique but it's the best I could find by just reading the docs.
Note: This will overwrite the original object.
Disclaimer: I'm not an active lodash user.
// Setup example data
var gillFamily = [{
name: 'john',
age: 20
}, {
name: 'richard',
age: 27
}, {
name: 'debbie',
age: 55
}, {
name: 'dan',
age: 25
}, {
name: 'robin',
age: 60
}];
// Display the initial values
document.getElementById("start").innerText = JSON.stringify(gillFamily);
// Append family name on the current family member's name
var gillFamilyWithLastNames = _.each(gillFamily, function(el) {
// Append 'Gill' to the end of the existing name
el.name += ' Gill';
});
// Show the results
document.getElementById("end").innerText = JSON.stringify(gillFamilyWithLastNames);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
<p>Starting Data</p>
<pre id="start"></pre>
<p>Ending Data</p>
<pre id="end"></pre>

Lodash sortByOrder with provided alphabet

I need to sort an array of objects by provided keys. Sorting must be case-insensitive and use provided alphabet. For example let's take initial data that looks like this:
var notOrdered = [{
date: "11-12-2015",
name: "Tomasz",
age: 50,
products: "FDGS",
rate: 500
}, {
date: "12-11-2015",
name: "Łukasz",
age: 54,
products: "ŁBDGS",
rate: 110
}, {
date: "11-12-2015",
name: "Jan",
age: 24,
products: "ŻDGS",
rate: 1000
}, {
date: "11-12-2015",
name: "Łucja",
age: 18,
products: "AEBDGS",
rate: 50
}];
var keys = ["date", "rate", "name"];
var directions = [true, false, true];
var alphabet = '01234567989aąbcćdeęfghijklłmnńoóprsśtuvwxyzźż'
So the result I'm looking for is:
var ordered = [{
date: "11-12-2015",
name: "Łucja",
age: 18,
products: "AEBDGS",
rate: 50
}, {
date: "11-12-2015",
name: "Jan",
age: 24,
products: "ŻDGS",
rate: 50
}, {
date: "11-12-2015",
name: "Tomasz",
age: 50,
products: "FDGS",
rate: 500
}, {
date: "12-11-2015",
name: "Łukasz",
age: 54,
products: "ŁBDGS",
rate: 110
}];
Using lodash's sortByOrder function var ordered = _.sortByOrder(notOrdered, keys, directions) takes care of sorting using provided keys and directions - one after another. And it works great. What I need now is to use provided alphabet order instead of the default one and make comparisons case-insensitive.
All the chars that are not listed in the provided alphabet should be compared the default way. I cannot use localCompare, because I need to support old IE and mobile browsers.
The question is: can I somehow make lodash's sortByOrder function to use custom alphabet and if so, how to do it?

Categories

Resources