Convert Papaparse rows to objects - javascript

Does papaparse support return an array of object instances that are keyed by the header columns?
For example I have a CSV file like this:
sku, location, quantity
'sku1', 'Chicago', 3
'sku2', 'New York, 4
I'd like the array returned by papaparse to look like this:
[{sku: 'sku1', location: 'Chicago', quantity: 3}, ...]
This should also be possible:
results[0].sku == 'sku1'
results[1].quantity == 4

Try adding header: true to the config parameter.
From the docs:
header: If true, the first row of parsed data will be interpreted as field names. An array of field names will be returned in meta, and each row of data will be an object of values keyed by field name instead of a simple array. Rows with a different number of fields from the header row will produce an error. Warning: Duplicate field names will overwrite values in previous fields having the same name.
For example:
Papa.parse('./data.csv', {
download: true,
header: true, // gives us an array of objects
dynamicTyping: true,
complete: ({ data }) => console.log(data),
});
given your data should yield
[
{
sku: 'sku1',
location: 'Chicago',
quantity: 3,
},
{
sku: 'sku2',
location: 'New York',
quantity: 4,
},
]

It is possible by few line in simple javascript working example:
My csv file data:
sku, location, quantity
'sku1', 'Chicago', 3
'sku2', 'New Yoark, 4

Related

How to get certain number in this JavaScript array? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
I'm new to JavaScript so my array mapping skills are bad, how would I find the assetid which is 47243781293 in this array? Thank you.
EconItem {
appid: 440,
contextid: '2',
assetid: '4723781293',
classid: '2674',
instanceid: '11040547',
amount: 1,
missing: false,
currency: false,
background_color: '3C352E',
icon_url: '...',
icon_url_large: '...',
tradable: false,
actions:
[ { link: 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=5002&lang=en_US',
name: 'Item Wiki Page...' } ],
name: 'Refined Metal',
name_color: '7D6D00',
type: 'Level 3 Craft Item',
market_name: 'Refined Metal',
market_hash_name: 'Refined Metal',
commodity: false,
market_tradable_restriction: 7,
market_marketable_restriction: 0,
id: '4723781293',
fraudwarnings: [],
descriptions: [],
owner_descriptions: [],
owner_actions: [],
tags: [],
marketable: false
}
I think you mean to ask "..which is 47243781293 in this JSON" and not "...which is 47243781293 in this array". The object which you pasted above is a JSON representation. If that's what you are meaning to ask, please read below -
Given that the key id will always be present and the object value pasted above is assigned to variable EconItem, I would try something like this
If( EconItem['id'] === '47243781293' )
{
console.log('Asset id: 47243781293 present in the JSON object');
}
If you are not sure that key id will always be present in the object, I would first check for the key to be present using Object.keys(). Details can be found here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Hope that helps!
To get a value from the object, just refer to it's key: EconItem.assetid
For objects with multiple results, You can iterate on it, and output a particular value from your Object like so:
for (var i in EconItem)
{
console.log(EconItem[i].assetid);
//do more here
}
to find this in an array, you use filter which is a function that ships with every javascript array, using your object above for each EconItem
let array = [EconItem, EconItem]
search = array.filter(eachItem=>eachItem.assetId==='47243781293');
--> returns an array of items with assetId as 47243781293, now the first item should be your EconItem, i.e search[0];
Check example snippet
var items = [{
appid: 440,
contextid: '2',
assetid: '4723781293'
}, {
appid: 441,
contextid: '2',
assetid: '4723781292'
}];
// now we search with this 4723781293
var search = items.filter(function(item){
return item.assetid === '4723781293';
});
//show our search result
alert ("item appid is:"+search[0].appid+", context:"+search[0].contextid+", assetid:"+search[0].assetid);

Using ng-options with JSON data

I'm still trying to find my way with AngularJS. I have a JavaScript code that uses URL to return JSON data as an array. I need help with populating the same data in select using ng-options.
data to populate on the select
This isn't how you ask for help, but nevermind. Given a JSON object like this
var JsonArray = [{
id: 1,
name: 'Jane',
address: 'Jane\'s house'
}, {
id: 2,
name: 'Jill',
address: 'Jill\'s house'
}, {
id: 3,
name: 'John',
address: 'John\'s house'
}, {
id: 4,
name: 'Jack',
address: 'Jack\'s house'
}];
When you want to use ng-select with ng-options, you need to specify 3 required fields :
your table
the name that every object will take (like a for ... each loop)
The property you want to see in your options
You also can specify a track by property to give your options a given value.
<select ng-model="mySelect" ng-options="object.name for object in JsonArray track by object.id"></select>
Now, use the last piece of code, and inspect it in a brwoser. You will understand everything.
For reference :
<select ng-model="mySelect" ng-options="[object].[chosenProperty] for [object] in [yourArray] track by [object].[property]"></select>

Traverse Nested JSON Objects - YUI AutoComplete

Say I have two different unrelated JSON Objects returned from separate AJAX requests
content: [{...}]
0: {...}
userId: "22"
name: "Kevin Johnson"
Manager: {…}
managerId: "123"
name: "Ryan Burke" //will be set as "searchValue"
content: [{...}]
0: {...}
companyId: "345"
companyName: "Trucks-R-Us" //will be set as "searchValue"
Building: {…}
buildingId: "5"
section: "North-West"
The attributes I've marked will be stored in a variable called searchValue (note they are not on the same level)
Can I access searchValue using YUI's AutoComplete plugin to locate the attributes I've specified using the combination of resultListLocator and resultTextLocator, regardless of what the key of the attribute is named or how nested the attribute is?
var autoComplete = new Y.AutoComplete({
inputNode: '#search-string',
resultListLocator: 'content',
resultTextLocator: function( return /** Find searchValue within nested Object**/),
resultHighlighter: 'phraseMatch',
maxResults: 10
});
Short version: Can my objects be traversed until a match to searchValue is found?
Can elaborate if this isn't enough detail

Angular chosen is not binding Array of Objects

I'm using angular chosen plugin for selecting an attribute on any select element.
My data is in this format:
$scope.pets = [
{
id: '1',
name: 'Dog',
desc:"Something"
},
{
id: '2',
name: 'Cat',
desc:"Something"
},
{
id: '3',
name: 'Rat',
desc:"Something"
}
];
And the angular choosen implementation for displaying the name using ng-options is:
<select multiple ng-model="myPets" ng-options="r as r.name for r in pets" chosen>
I'm able to get the drop down using ng-options for the above data like this,
But how can I bind the default values into the angular choosen input box if my ng model is bind to the following object:
$scope.myPets= {
id: '6',
name: 'Pig',
desc:"Something"
},
You can set the default values in the controller by using
$scope.myPets= [$scope.pets[0], $scope.pets[5]];
Compared to what you were thinking you need to use an array [] because you are using select multiple. You also have to directly refer to the existing objects or angular/javascript won't recognize the connection.

Prevent Javascript function running out of memory because too many objects

I'm building a web scraper in nodeJS that uses request and cheerio to parse the DOM. While I am using node, I believe this is more of a general javascript question.
tl;dr - creating ~60,000 - 100,000 objects, uses up all my computer's RAM, get an out of memory error in node.
Here's how the scraper works. It's loops within loops, I've never designed anything this complex before so there might be way better ways to do this.
Loop 1: Creates 10 objects in array called 'sitesArr'. Each object represents one website to scrape.
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description'
},
// ... x10
]
Loop 2: Loops through 'sitesArr'. For each site it goes to the homepage via 'request' and gets a list of category links, usually 30-70 URLs. Appends these URLs to the current 'sitesArr' object to which they belong, in an array property whose name is 'categories'.
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description',
categories: [
{
name: 'shoes',
url: 'www.basedomain.com/shoes'
},{
name: 'socks',
url: 'www.basedomain.com/socks'
} // x 50
]
},
// ... x10
]
Loop 3: Loops through each 'category'. For each URL it gets a list of products links and puts them in an array. Usually ~300-1000 products per category
var sitesArr = [
{
name: 'store name',
baseURL: 'www.basedomain.com',
categoryFunct: '(function(){ // do stuff })();',
gender: 'mens',
currency: 'USD',
title_selector: 'h1',
description_selector: 'p.description',
categories: [
{
name: 'shoes',
url: 'www.basedomain.com/shoes',
products: [
'www.basedomain.com/shoes/product1.html',
'www.basedomain.com/shoes/product2.html',
'www.basedomain.com/shoes/product3.html',
// x 300
]
},// x 50
]
},
// ... x10
]
Loop 4: Loops through each of the 'products' array, goes to each URL and creates an object for each.
var product = {
infoLink: "www.basedomain.com/shoes/product1.html",
description: "This is a description for the object",
title: "Product 1",
Category: "Shoes",
imgs: ['http://foo.com/img.jpg','http://foo.com/img2.jpg','http://foo.com/img3.jpg'],
price: 60,
currency: 'USD'
}
Then, for each product object I'm shipping them off to a MongoDB function which does an upsert into my database
THE ISSUE
This all worked just fine, until the process got large. I'm creating about 60,000 product objects every time this script runs, and after a little while all of my computer's RAM is being used up. What's more, after getting about halfway through my process I get the following error in Node:
FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
I'm very much of the mind that this is a code design issue. Should I be "deleting" the objects once I'm done with them? What's the best way to tackle this?

Categories