I have a fairly complex array generated from Google's natural language API. I feed it a paragraph of text and out comes lots of language information regarding such paragraph.
My end goal is to find "key words" from this paragraph, so, to achieve this I want to put all the "entities" into a flat array, count the duplicates, and then consider words with the highest amount of duplicates to be "key words". If it doesn't find any then I'll cherry pick words from entities I consider most significant.
I already know the entities that could exist:
var entities = [
'art',
'events',
'goods',
'organizations',
'other',
'people',
'places',
'unknown'
];
Here is an example structure of the array I'm working with.
input = [
{
language: {
entities: {
people: [
{
name: "Paul",
type: "Person",
},
{
name: "Paul",
type: "Person",
},
],
goods: [
{
name: "car",
type: "Consumer_good",
}
], //etc
}
}
}
];
output = ["Paul", "Paul", "car"...];
My question is - what is the best way to convert my initial array into a flat array to then find the duplicates without using a whole bunch of FOR loops?
There is no way around loops or array functions if you work with dynamic input data.
You can access all the values using this format:
input[0]["language"]["entities"]["people"][0].name
input = [
{
language: {
entities: {
people: [
{
name: "Paul",
type: "Person",
},
{
name: "Paul",
type: "Person",
},
],
goods: [
{
name: "car",
type: "Consumer_good",
}
], //etc
}
}
}
];
console.log(input[0]["language"]["entities"]["people"][0].name);
Then you could do something like this:
for (var entry in input[0]["language"]["entities"]) {
console.log(entry);
}
OR, if I understood you wrong,
You can use this to turn the javascript Object into an array using this (requires jquery):
var myObj = {
1: [1, 2, 3],
2: [4, 5, 6]
};
var array = $.map(myObj, function(value, index) {
return [value];
});
console.log(array[0][0]);
console.log(array[0]);
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This will output
1
[1, 2, 3]
[[1,2,3],[4,5,6]]
You could iterate through input.language.entities in a recursive way and collect all the .name properties into an array. Then you have only one for loop :-).
After doing that, you can iterate through it to find the duplicates. If you sort it alphabetical before it is easier (if two or more consecutive entries are equal, there are duplicates).
But it could be a bit dangerous if google changes the api or if it delivers crap data because of a malfunction.
Isn't input.language.entities already flat enough to work with it?
I ended up doing something like this. It's not pretty but it gets the job done.
var result = [];
var known_entities = ['art','events','goods','organizations','other','people','places','unknown'];
for(i=0; i < known_entities.length; i++){
var entity = known_entities[i];
if(language.entities[entity]){
for(var j in language.entities[entity]){
var word = language.entities[entity][j].name
result.key_words.push(word);
}
}
}
Related
I currently have an array of objects that look like this but with a ton more entries,
[
{
owningrep: "Some name",
id: 1,
emails: "email#address.com;shane#emails.com"
},
{
owningrep: "Other name",
id: 2,
emails: "me#emailstuff.com"
}
]
I also provide the option to import a CSV of emails, which I then take all of the values and put them in an array.
My question is, given this array of objects, and this array of CSV values, how would I then be able to filter the array of objects to NOT include objects where any of the emails in the csv appear in the email value? Keep in mind some objects might have one email, others might have multiple separated by a semicolon.
Thanks in advance
I've attempted to simply filter the array with includes, but that only seems to cut off a few entries.
let inc = inclusionsList.value.length > 0 && inclusionsList.value['0'] != 0 formatDataAsArray(data).filter(d => _.includes(inclusionsList.value, d.id)) : data;
let fromCSV = formatDataAsArray(inc).filter(i => !_.includes(exclusionCSV.value, i.Emails));
Ultimately what I want to do is take an array of objects like:
[
{
owningrep: "Some name",
id: 1,
emails: "email#address.com;shane#emails.com"
},
{
owningrep: "Other name",
id: 2,
emails: "me#emailstuff.com"
}
]
And an array of emails like:
["crona.audreanne#denesik.com", "minerva.leannon#pfannerstill.info", "barton.eloise#yahoo.com", "rachelle.rau#hotmail.com", "ihaley#morar.com", "ggleichner#kirlin.info", "keely.ledner#yahoo.com", "jerel05#yahoo.com", "eleonore35#murphy.info", "ivory56#hotmail.com", "email#address.com"]
And filter the array of objects so that the only objects left are ones whose email key does not include any of the emails in the array.
If I understrand well your problem and your data here is my proposition:
const exclusionCSV = {value: ['mail1', 'me#emailstuff.com']}
const inclusionList = {value: [
{
owningrep: "Some name",
id: 1,
emails: "email#address.com;shane#emails.com"
},
{
owningrep: "Other name",
id: 2,
emails: "me#emailstuff.com"
}
]}
const setExclusionMail = new Set(exclusionCSV.value)
const result = inclusionList.value.filter(({emails}) => {
const mails = emails.split(';')
return !mails.some(m => setExclusionMail.has(m))
})
var object = [{id:1, users:[{name:'john', name:'smith', name:'frank'}, {id:2, users:[{name:'lary'}, {name:'steve'}, {name:'joe'}]}]}]
output:
allUsers = [{name:'john'}, {name:'smith'}, {name:'frank'}, {name:’lary'}]
need answer in javascript. and also using lodash
I have not used lodash, but from the documentation I found a "flatten" -method, take a look here:
https://lodash.com/docs/4.17.5#flatten
Flattens array a single level deep.
I hope that helps you.
Edit: Ok here is the code example (Your curly braces were little off so I fixed them in this example. I hope this is what you were looking for.
But basically with map, get the users array from the individual object, then flatten the results after that.
var object = [
{
id:1,
users: [{name:'john'}, {name:'smith'}, {name:'frank'}]
},
{
id:2,
users: [{name:'lary'}, {name:'steve'}, {name:'joe'}]
}
];
const flattenUsers = lodash.flatten(object.map(id => id.users));
console.log(flattenUsers);
/*
Output:
[ { name: 'john' },
{ name: 'smith' },
{ name: 'frank' },
{ name: 'lary' },
{ name: 'steve' },
{ name: 'joe' } ]
*/
I need to know the best way to get following results
courseFrequency : [
{
'courses': [
'a.i'
],
'count' : 1
},
{
'courses': [
'robotics'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i','robotics'
],
'count' : 1
}
]
from following json data.
arr = [
{
'courses': [
'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
],
'courses': [
'software engineering', 'a.i','robotics'
]
}];
Basically i need to find out the unique courses and their frequency. What is the most optimal way to do that ?
const hash = {}, result = [];
for(const {courses} of arr){
const k = courses.join("$");
if(hash[k]){
hash[k].count++;
} else {
result.push(hash[k] = { courses, count : 1 });
}
}
Simply use a hashmap to find duplicates. As arrays are compared by reference, we need to join it to a string for referencing ( note that this will fail if a coursename contains the joining symbol ($))
There both of them are best for area relates to them.These concepts are heaving their own property and methods to accomplish a certain task like JSON used for data transfer and cross browsing aspect as the common type data value.Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
JSON is a representation of the data structure, it's not an object or an array.
JSON can be used to send data from the server to the browser, for example, because it is easy for JavaScript to parse into a normal JavaScript data structure.for doing an action on JSON data you need to convert it into an object which is also seamed some property like ARRAY.
Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
Relative link
Relative link
Using Fuse.js, I need to weight individual item for a better ranking in search results. For instance, how do I make sure "Paris France" has the biggest score for a "Paris" query with the data below?
places = [{
name: 'Paris, France'
weigth: 10
},{
name: 'Paris, Ontario'
weigth: 2
},
{
name: 'Paris, Texas'
weigth: 1
}]
As far as I am aware, there are no methods built into Fuse.js to do this. The weight property is meant to be applied to properties which are being searched (in the options object), rather than to the object that is being searched (as seen in the example here.
What I might suggest is writing a function to sort this yourself. So once you get your results array back, after the search, perform an Array.sort() on it yourself (documentation here).
For example...
//Your places object
var places = [
{
name: 'Paris, Texas',
weight: 2
},
{
name: 'Paris, France',
weight: 10
},
{
name: 'Paris, Texas',
weight: 1
}
];
//Your search options
var options = {
keys: [
"name"
]
};
var fuse = new Fuse(places, options); // "list" is the item array
var result = fuse.search("Paris");
//Once you have got this result, perform your own sort on it:
result.sort(function(a, b) {
return b.weight - a.weight;
});
console.log('Your sorted results:');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.1.0/fuse.min.js"></script>
I'm currently working on a small application where I have to loop through an enormous array of objects. What would be the most efficient method to perform this?
var array = [
{
id: "1",
name: "Alpha"
},
{
id: "2",
name: "Beta"
},
...
];
I'd like to get each object where name equals "Alpha". I'm currently using a simple if statement to filter the objects with a different name value out, but I wonder if there's a more efficient way to do this, performance-wise.
It's worth to mention that I'll push the matching results into a new array.
No, there is no more efficient way.
The alternative is to build and maintain some kind of internal data structure which allows you to find the desired elements faster. As usual, the trade off is between the work involved in maintaining such a structure vs the time it saves you.
I don't have any way about which I would know it's more effective.
But if you had your objects ordered by name you could stop your search imideatly upon reaching an object whose name is not equal to "Alpha".
To find the first object you're looking for you can use binary search and from this Object you go up and down until at both ends you reach an object which isn't named "Alpha" or the end of array.
This is only a way of optimizing and will require time to sort the array and also will take more time when adding an element.
There's a JavaScript function exactly for this kind of task. Filter
From the Docs
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
Here is a small example by code for getting all element from array which has a certain 'name' field:
const arr = [
{name: 'Abc'},
{name: 'Xyz'},
{name: 'Lmn'},
{name: 'Xyz'},
{name: 'Xyz'}
];
let response = findByName('Xyz');
console.log(response);
function findByName (name) {
return arr.filter((element) => {
return element.name = name;
});
}
If you need more than one time a collection with a given name, you could use an object with the names as hashes and have instantly access to the items.
var array = [{ id: "1", name: "Alpha" }, { id: "2", name: "Beta" }, { id: "3", name: "Beta" }, { id: "4", name: "Gamma" }, { id: "5", name: "Beta" }, { id: "2", name: "Alpha" }],
hash = Object.create(null);
array.forEach(function (a) {
if (!hash[a.name]) {
hash[a.name] = [];
}
hash[a.name].push(a);
});
console.log(hash);
.as-console-wrapper { max-height: 100% !important; top: 0; }