How to merge array of arrays in one array (in JavaScript)? - javascript

I have an array of objects in JavaScript. Each object contains property named "myPropArray", which is actually another array. Here is the structure of this array:
let myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
What I need is to take all arrays under this property and to merge them all in one array, without duplicates (in JavaScript).
Here is my implementation (using lodash):
_.map(myBaseArray , 'myPropArray')
Which is actually returning the following result:
[
["s1", "s2"],
["s2", "s3"]
]
But what I want to accomplish is:
["s1", "s2", "s3"]
Also (if possible) I'm trying to avoid for-each loops, since this needs to me optimized as much as possible and I'm wondering if can be done using lodash mappers or other similar function?
There are already some solutions from this stage where I got (as the solution here) but I would like to find a solution tho this problem which will be specific for my "array or objects which contains properties of type Array"
.

Extract the property's value with Array.map(), flatten by spreading into Array.concat(), and use a Set to get unique values. Spread the Set back to an array:
const myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
const result = [...new Set([].concat(...myBaseArray.map((o) => o.myPropArray)))]
console.log(result)
The lodash way would be to use _.flatMap(), and _.uniq():
const myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
const result = _.uniq(_.flatMap(myBaseArray, 'myPropArray'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Related

Using lodash isEqual() to compare single object property with matching properties from large array with multiple objects

I've looked at lodash documentation and played around with comparing simple objects. I've also found a number of explanations online for comparing entire objects and other types of comparisons, but I want to compare one property value in a single object with the values of all properties of a certain name in a large array with multiple objects.
Is lodash smart enough to do this as is, and, if so, what would be the proper syntax to handle this? Or do I need some sort of loop to work through the larger object and recursively compare its properties of a certain name with the small object property?
The javascript comparison I'm looking for would be something like this, but I don't know how to indicate that I want to compare all itemURL properties in the large array:
// guard clause to end the larger function if test is true, any match found
if (_.isEqual(feedItem.link, rssDataFileArr.itemURL)) {
return;
}
Small object example:
const feedItem = {
link: 'https://news.google.com/rss/search?q=nodejs',
otherProperty: 'whatever'
}
Large array of objects example:
const rssDataFileArr = [
{
"itemURL": "https://news.google.com/rss/search?q=rss-parser",
"irrelevantProperty": "hello"
},
{
"itemURL": "https://news.google.com/rss/search?q=nodejs",
"irrelevantProperty": "world"
},
{
"itemURL": "https://news.google.com/rss/search?q=javascript",
"irrelevantProperty": "hello"
}
]
Any and all help appreciated.
As per suggestion in comment, I went with a built-in javascript method instead of lodash. I used some() because I only needed a true/false boolean result, not a find() value.
const feedItem = {
link: 'https://news.google.com/rss/search?q=nodejs',
otherProperty: 'whatever',
};
const rssDataFileArr = [
{
itemURL: 'https://news.google.com/rss/search?q=rss-parser',
irrelevantProperty: 'hello',
},
{
itemURL: 'https://news.google.com/rss/search?q=nodejs',
irrelevantProperty: 'world',
},
{
itemURL: 'https://news.google.com/rss/search?q=javascript',
irrelevantProperty: 'hello',
},
{
itemURL: 'https://news.google.com/rss/search?q=nodejs',
irrelevantProperty: 'world',
},
];
const linkMatch = rssDataFileArr.some(
({ itemURL }) => itemURL === feedItem.link
);
// guard clause to end the larger function if test is true, any match found
if (linkMatch) {
console.log('linkMatch is true');
return;
}

Filter elements inside an array inside an object inside an array in Javascript returns somes values undefined

I am using a Japanese dictionary API, which allows me to put in a term and return a list of nouns, verbs, adverbs, and others. This is a result of the API. The API returns an array with a list of objects. Each object has its own arrays and in one of these arrays, there is the array with the information I need, like this:
{"data":[
{
"attribution": {"jmdict": true, "jmnedict": false},
"senses": [
{
"english_definitions": [
"sleep"
],
"parts_of_speech": [
"Noun",
"Ichidan verb"
]
},
{
"english_definitions": [
"Sleep"
],
"parts_of_speech": [
"Ichidan verb", "intransitive verb"
]
}
]
}
]
}
I want to filter the result to show only those results that are verbs, excluding the other results.
This is my code to do the filter:
verbArray = jsonData.data
let verbs2 = verbArray.filter(e => e.senses[0].parts_of_speech[0].includes('verb'));
//console.table(verbs2);
This code does the filter and gives me only those results that have the word verb in the string. But only if it is in position one of the array. If the term is in position two or three, will not return it
You can use
let verbs2 = verbArray.filter(e => e.senses[0].parts_of_speech.some(pos=>pos.includes('verb')));
if you only want to iterate all parts of speeches. The Array.prototype.some() will check all parts of speech for the term verb.
If you also want to iterate senses, you can use
let verbs2 = verbArray.filter(e => e.senses.some(sense=> sense.parts_of_speech.some(pos=>pos.includes('verb'))));

Get array from nested json value objects

I've been searching an answer for that but didn't found it.
I have an array like:
const data2 = [{
"abc":{
companyCity:"Cupertino",
conpanyName:"Apple"
}
},
{
"def":{
companyCity:"Mountain View",
conpanyName:"Google"
}
}
]
And I'd like to convert to and array like omiting the parent keys:
const data3 = [
{
companyCity:"Cupertino",
companyName:"Apple",
},
{
companyCity:"Mountain View",
companyName:"Google"
}
]
Perhaps, libraries like lodash have a method to achieve that, but didn't find it. Any help would be very appreciated :)
Iterate the array with Array.flatMap() (or lodash's _.flatMap()), and get the an the inner object of each item using Object.values() (or _.values()):
const data = [{"abc":{"companyCity":"Cupertino","conpanyName":"Apple"}},{"def":{"companyCity":"Mountain View","conpanyName":"Google"}}]
const result = data.flatMap(Object.values)
console.log(result)

Accessing JSON value with unique named subarrays

I want to sort a JSON array based on time value in a subarray with the key names of the subarrays being named uniquely.
I'm searching for the method to access key, value update_time of every element in Products so I can use that value in a sorting script.
I have tried sorting the array but can not determine how to access the key, values of the subarrays
Expected behavior should be that every unique_keyname_# element is available for sorting and is sorted for further processing in JavaScript. Ultimately with the newest unique_keyname_# as the first element in a list, based on the update_time key.
var obj = {
"company": {
"department_1": {
"Products": {
"unique_keyname_1": {
"product_owner": "co-worker-1",
"update_time": "unix_timestamp_1"
},
"unique_keyname_5": {
"product_owner": "co-worker-4",
"update_time": "unix_timestamp_45"
},
"unique_keyname_8": {
"product_owner": "co-worker-2",
"update_time": "unix_timestamp_5"
}
}
},
"department_2": {
"Products": {
"unique_keyname_3": {
"product_owner": "co-worker-1",
"update_time": "unix_timestamp_21"
},
"unique_keyname_6": {
"product_owner": "co-worker-2",
"update_time": "unix_timestamp_7"
},
"unique_keyname_4": {
"product_owner": "co-worker-3",
"update_time": "unix_timestamp_75"
}
}
}
}
}
I solved the issue by writing an intermediate script in python which makes the API response a valid array. From there it was fairly easy to sort the data.
Thanks for the replies confirming the data itself was deliverd to me in an inappropriate format!
regards
In your example, there are no arrays.
Anyway, in Javascript you can access a node using . like:
obj.company.department_1.Products.unique_keyname_1
Or using [] which gives you more freedom to use costume fields
obj["company"]["department_1"]["Products"]["unique_keyname_1"]
// can also be more dynamic as:
obj["company"]["department_"+ department_counter]["Products"]["unique_keyname_" + keyname_counter]
Is there a possibility that you will change the structure of your JSON? to make it more manangeable ?
if so, i would recommend the folowing structure:
var products = [
{
department: 'SomeDepartment',
productName: 'Something',
productOwner: 'Someone',
update_time: 'Sometime'
}
]
Then you can sort the array easy using Array.sort()
for the sort topic use this : Sort array of objects by string property value

Array filteration and Extraction of data and append to new Array

I have an array with nested array
I want the data to append in a new array.
For the data extraction or filtration what method's i have to use, using library such as lodash
DATA
[
[
{
_id: 588d9b8a608f2a66c298849f,
email: 'sd#',
password: '$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC',
isJobSeeker: true,
__v: 0,
lastName: 'shrestha',
firstName: 'manish',
isSeeker: true
}
],
[
{
_id: 588dbb4f7a48ce0d26cb99fd,
jobId: [Object],
seekerId: 588d9b8a608f2a66c298849f,
employerId: 588d7d6c0ec4512feb819825,
__v: 0,
}
]
]
REQUIRED DATA
[
{
_id: 588d9b8a608f2a66c298849f,
email: 'sd#',
password: '$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC',
isJobSeeker: true,
__v: 0,
lastName: 'shrestha',
firstName: 'manish',
isSeeker: true
},
jobId: [{}, {}, {}] // ARRAY WITH OBJECTS
]
also i want to change the jobId key to other key of custom string as jobs
Following is my attempt:
console.log('Data filteration', data);
const filteredData = [];
filteredData.push(data[0][0]);
data[1].forEach((i) => {
filteredData[0].jobs = i.jobId
});
console.log('filteredData', filteredData);
First you should clean you data to have a better structure.
[
[
{ ... }
],
[
{ ... }
]
]
In this datastructure, its difficult to understand what does inner arrays signify. Instead you should use an object. That would define the purpose of array and make your code more readable.
var data=[[{_id:"588d9b8a608f2a66c298849f",email:"sd#",password:"$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC",isJobSeeker:!0,__v:0,lastName:"shrestha",firstName:"manish",isSeeker:!0}],[{_id:"588dbb4f7a48ce0d26cb99fd",jobId:["test","test1"],seekerId:"588d9b8a608f2a66c298849f",employerId:"588d7d6c0ec4512feb819825",__v:0}]];
var cleanedData = {
userData: data[0],
userJobMap: data[1],
}
var result = cleanedData.userData.reduce(function(p,c){
if(c.isJobSeeker){
var job = cleanedData.userJobMap.filter(x=> x.seekerId === c._id);
// To copy object and not reference
var t = Object.assign({}, c, { jobId: job[0].jobId });
p.push(t)
}
return p
}, [])
console.log(result)
References
Array.map is a tool that iterates over all elements and return different value say a single property of return double value of all numbers in array. Note, this will yield an array of same size.
Array.filter on the other hand is use to filter array based on condition. This will return a subset of original data but elements will be same. You cannot change element structure.
Array.reduce is a tool that address cases where you need to return selected elements with parsed value. You can achieve same by chaining .filter().map() but then its an overkill as it would result in O(2n).
Object.assign In JS objects are passed by reference. So if you assign an object to a variable, you are not copying entire object, but only reference. So it you change anything in this variable, it will also reflect in original object. To avoid this, you need to copy value. This is where Object.assign comes. Note, its not supported by old browsers. For them you can check following post - What is the most efficient way to deep clone an object in JavaScript?
Note: All array functions are part of functional programming paradigm and are used to make your code more readable and concise but they come at an expense of performance. Traditional for will always perform faster then them. So if you want to focus on performance, always try to use for (though difference is very small but can add up for multiple cases and become substantial)

Categories

Resources