getting list of data from list of list json vue - javascript

I need to get filter data but it works when its only list of data not list in list.
There is some lists of object name starting with number1 with list but need the data inside number and save it in array. Using smart table vuejs so if i get json data it will work since i did similar things.
This is my code
let result = [];
let response = await this.$axios.post("api");
for (item in response.data.list){
result.push(response.data.list[item][0];
}
{
"list": {
"number1": [
{},
],
"number2": [
{}
],
"number3": [
{}
],
etc...
}
<v-table
//it is within thead and
:data="result"
:filters= "filters">
<input v-model="filters.name.value"
<v-table/>
data:function(){
return {
result: [];
filters: {
... //other fields same as below
name: { value: "", keys: ["name"] },
...
},
}
}

Related

adding json key names to a json values and changing key names

I have a json string that is in the format:
[
{
clientIDs:
"WELL #6",
analyteIDs:
[
"7440-62-2",
"7440-28-0"
]
}
]
I need to convert this to:
[
{
header:
"WELL #6",
items:
[
header: "7440-62-2",
header: "7440-28-0"
]
}
]
The values without a key name are throwing me off.
Unfortunately js cannot store a key value arrays, instead you have to use an object storing key and value. So the closes result you can achieve is following:
[
{
header:
"WELL #6",
items:
[
{ header: "7440-62-2" },
{ header: "7440-28-0" }
]
}
]
For that your steps will be following:
Assuming you have an array of objects.
Assuming the keys you want to change are static and will always exist in the objects
const myObjects = [
{
clientIDs:
"WELL #6",
analyteIDs:
[
"7440-62-2",
"7440-28-0"
]
}
]
myObjects.map((myObj) => {
myObj['header'] = myObj.clientIDs;
myObj['items'] = myObj.analyteIDs.map((item) => {
return { header: item }
});
// Keep in mind, if keys are dynamic and does not exist in some objects then this will fail
delete myObj['clientIDs'];
delete myObj['analyteIDs'];
});
console.log(myObjects);

Creating an object from API data and adding it to an array dynamically

I have a data set that I'm pulling in from a database. It's one dimensional and I basically need to make it more structured. I refer to it as "flat".
I need to display a heading, and items under that heading that are related to the heading.
The data comes in as having and section_name (the heading) and item_name (items) and other data unique to each item like download URLs etc.
item_name(item)_______section_name(header)
first_________________Funds
second________________Funds
third_________________Funds
fourth________________Literature
fifth_________________Literature
sixth_________________Literature
seventh_______________Literature
eighth________________DueDilligence
I don't know what any of the names will be for the items or sections, or how many items, sections, or items per section. As I said, it's very flat. This needs to be fully dynamic which is why this is complicating things for me.
Here is what I've done.
API call to retrieve data. Store data in a state as an array (it comes in as an array of objects).
I create an empty array to store my newly structured data.
I loop through the data with a foreach.
I create a new object for my new data to add to the new array so I can loop over it later.
I first check to make sure the data exists.
To create the headers I check to see if my new empty array is actually empty OR my section_name is not the same as the last one.(in the original data array I got from the API call)
I store the section_names as an object in the new array (newArray.push(newObject)
I've gotten this far. Now I need to take the item_names that correlates to the section_names and store them in the object under each header name, or at least in the same index.
_generateInfo() {
let dataArray = this.state.stepTwoData
let newArray =[]
dataArray.forEach(function(item, index) {
let newObject = {}
if (index > 0) {
if (newArray.length === 0 || item.investor_portal_section_name !== dataArray[index -1].investor_portal_section_name) {
newObject["name"] = item.investor_portal_section_name
newObject["items"] = []
newArray.push(newObject)
}
})
console.log(newArray)
}
I tried pushing the items to the "number" array on my new object and that doesn't seem to work properly. Sometimes it will duplicate my newObject.name
Checking if the newObject.name === the section_names in the array and push it to the "number" array in my new object just creates new key-value pairs so it's still not correlating.
I tried looping through again in the if statement and if section_name === newObject.name then create a newObject and push it, but it would only push one of the items repeatedly instead of going through all of them.
I need to loop through and create a header (one header per different section_name). Then add each item that corresponds to the section_name to it. like this
[
{section_name(header): "Funds",
items: [
{
name: item_name,
sku: item_sku,
url: item_url
},
{
name: item_name,
sku: item_sku,
url: item_url
}]
},
{section_name(header):"Literature",
items: [
{name: item_name,
sku: item_sku,
url: item_url
},
{
name: item_name,
sku: item_sku,
url: item_url
}]}
]
Using associative array (dictionary) to segregate you data itmes by categories will do the job.
I've drafted some POC code that illustrates the idea. The key element there is buildAssociativeArray function
const raw_data = [
{item_name: "first", section_name: "Funds"},
{item_name: "second", section_name: "Funds"},
{item_name: "third", section_name: "Funds"},
{item_name: "fourth", section_name: "Literature"},
{item_name: "fifth", section_name: "Literature"},
{item_name: "sixth", section_name: "Literature"},
{item_name: "seventh", section_name: "Literature"},
{item_name: "eighth", section_name: "DueDilligence"},
]
function buildAssociativeArray(data) {
const dictionary = {};
for (var i = 0; i < data.length; i++) {
const item = data[i];
const section = item.section_name;
var dictEntry = dictionary[section];
if (!dictEntry) {
dictEntry = [];
dictionary[section] = dictEntry;
}
dictEntry.push({
name: item.item_name,
// other fields like sku: item_sku or url: item_url may follow here
});
}
return dictionary;
}
const dictionary = buildAssociativeArray(raw_data);
console.log(dictionary);
/*
At this point
dictionary == {
"Funds": [
{
"name": "first"
},
{
"name": "second"
},
{
"name": "third"
}
],
"Literature": [
{
"name": "fourth"
},
{
"name": "fifth"
},
{
"name": "sixth"
},
{
"name": "seventh"
}
],
"DueDilligence": [
{
"name": "eighth"
}
]
}
*/
// Associcative array dictionary itself allows to further solve you task using for (var key in dictionary) {...} operator
// If however you need to obtain the data structure looking exactly like the one in your question you may go further with following function
function transformAssociativeArray(dictionary) {
const array = [];
for (var key in dictionary) {
const items = dictionary[key];
const newEntry = {
section_name: key,
items: items,
}
array.push(newEntry);
}
return array;
}
const array = transformAssociativeArray(dictionary);
console.log(array);
/*
At this point
array == [
{
"section_name": "Funds",
"items": [
{
"name": "first"
},
{
"name": "second"
},
{
"name": "third"
}
]
},
{
"section_name": "Literature",
"items": [
{
"name": "fourth"
},
{
"name": "fifth"
},
{
"name": "sixth"
},
{
"name": "seventh"
}
]
},
{
"section_name": "DueDilligence",
"items": [
{
"name": "eighth"
}
]
}
]
*/

How to Iterate Over Array and Stringify a Property

I am trying to accumulate a property of an array and write save it back to the array as a string to later be parsed with JSON.parse.
In the initial data set the items property is an array.
I would like to restructure the data such that items is a string of the objects.
Given something like:
[{
{
"n":"1",
"items": [0: "{"id":"id1","desc":"description1"}",
1: "{"id":"id2","desc":"description2"}"
2: "{"id":"id3","desc":"description3"}"]
},
{
"n":"2",
"items": [0: "{"id":"id4","desc":"description4"}",
1: "{"id":"id5","desc":"description5"}"
2: "{"id":"id6","desc":"description6"}"]
}
}]
Convert to:
[{
{
"n":"1",
"items": "[{"id":"id1","desc":"description1"}","{"id":"id2","desc":"description2"}","{"id":"id3","desc":"description3"}]"
},
{
"n":"2",
"items": "[{"id":"id4","desc":"description4"}","{"id":"id5","desc":"description5"}", "{"id":"id6","desc":"description6"}]"
}
}]
I had to clean up your data, so I will go with the assumption that the data structure I set below is what you actually meant.
const data = [
{
"n":"1",
"items": [
{"id":"id1","desc":"description1"},
{"id":"id2","desc":"description2"},
{"id":"id3","desc":"description3"}
]
},
{
"n":"2",
"items": [
{"id":"id4","desc":"description4"},
{"id":"id5","desc":"description5"},
{"id":"id6","desc":"description6"}
]
}
];
const newData = data.map(val => {
return Object.assign({}, val, {
items: JSON.stringify(val.items)
});
});
console.log(newData);
arr.map(({ items, ...rest }) => ({ items: JSON.stringify(items), ...rest }));

how to extract object array(results) from iron ajax response object

enter image description herebelow code snippet for getting json data from iron ajax call . i am able to get json object value in mapResponse.
i want to get the value of results array which is in json data and want to pass this results object value in another polymer component as a input attribute
code for loading data from iron ajax
<iron-ajax
id="originalData"
auto
url="{{originalDataURL}}"
handle-as="json"
last-response="{{originalData}}" on-response="mapResponse">
</iron-ajax>
Json file
{
"tags": [
{
"name": "test",
"results": [
{
"groups": [
{
"name": "type",
"type": "number"
}
],
"values": [
[
946890000000,
99.93584833,
3
],
[
946846800000,
99.94809842,
3
],
[
946803600000,
99.96034846,
3
],
[
946760400000,
99.97259848,
3
],
[
946717200000,
99.98484848,
3
]
],
"attributes": {}
}
],
"stats": {
"rawCount": 5
}
}
]
}
<script>
Polymer({
is: 'test-view',
properties: {
results: {
type: Array
},
mapResponse: function (data) {
var dummy = data.detail.response;
console.log("resposne is ",dummy);
results = dummy.results;
console.log("array is ",results);
},
i tried to get results array object value as above in console logs but getting undefined. here i am able to get the value of dummy where i am getting full json object (tags) i want to get only results array object from this (tags )object .
can anyone please suggest me how can i get only results array value ??
Thanks in advance.
It should be results = dummy.tags[0].results;
instead of results = dummy.tags[0].results;.
Or you can do results = dummy.tags.map(x=>x.results); if tags have multiple arrays of results.

Deepstream don't fetch data from RethinkDB table

Can't fetch data from table I've just created in rethinkDB.
I've create a new table in retinkDB - items.
And fill it with data:
r.db('test').table('items').insert([
{name: 'qqq'},
{name: 'www'},
{name: 'eee'}
])
BUT .getList() never returns table's data:
client.record.getList('items') // '.getEntries()' always return []
I don't really understand why .getList('items') didn't return data from items table.
I assume that this is because entries structure: when you are create entry via deepstream, entry's structure be like:
[
...
{
"_d": {
"id": "qqq"
"name": 'qqq'
},
"_v": 0,
"ds_id": "qqq"
}
...
]
But mine structure is just:
[
{
"id": 'qqq'
"name": 'qqq'
}
]
My question is: How to create table with data in rethinkDB (via script) that would we worked with deepstream?
deepstream is designed to manage data for you. Simply by saying
var bmw = ds.record.getRecord( 'cars/bmw' )
deepstream will create a table called cars and store an entry with a primary key of bmw in it. You wouldn't be expected to create the table yourself.
I've find a solition.
First of all table should be created with proper primaryKey === "ds_id":
r.db('test')
.tableCreate('items',
{
primaryKey: 'ds_id'
})
And after that you have to insert data with deepstream-like structure:
r.db('test').table('items').insert([
{
"_d": {
"id": r.uuid(),
"name": "qqq"
},
"_v": 0,
"ds_id": r.uuid()
}
])
P.S. to make _d.id equal to ds_id use this:
r.db('test').table('items').forEach(function(c) {
return r.db('test').table('items').get(c('ds_id')).update(function(row) {
return {_d: {id: row('ds_id')}};
});
})
This is stupid but I don't know how to do it in more elegant way.

Categories