Extracting some values from JSON objects in AngularJS - angularjs

I have a JSON text (posted below) and I want to extract name and channel_remote_number from each of its object inside its item array. Can someone please guide how can I do that?
JSON Text:
{
"xml":{
"version":"3.0.0",
"item_startidx":"0",
"total_items":"471",
"items_link":"https://example_url/",
"items":{
"item":[
{
"id":"36438",
"name":"A plus",
"type":"liveWMV",
"link":"https://example_url/",
"duration":"35000",
"channel_logo":{
"#cdata":"http://example_url/"
},
"channel_remote_number":"180",
"description":"A plus",
"response_link":"https://example_url/",
"restrict_link":"https://example_url/",
"play_time":"https://example_url/",
"protected":"no",
"program_listing":"https://example_url/",
"program_guide":"https://example_url/",
"electronic_program_guide":"https://example_url/",
"catchup_tv":"7",
"popup":{
"type":"blocking",
"message":"Temporary Down",
"buttons":{
"button":{
"type":"cancel",
"text":"OK"
}
}
},
"category_id":"12797",
"path":"Smart TV App>Live TV>Pakistani>Entertainment"
},
{
"id":"37669",
"name":"A plus",
"type":"liveWMV",
"link":"https://example_url/",
"duration":"35000",
"channel_logo":{
"#cdata":"http://example_url/"
},
"channel_remote_number":"180",
"description":"A plus",
"response_link":"https://example_url/",
"restrict_link":"https://example_url/",
"play_time":"https://example_url/",
"protected":"no",
"program_listing":"https://example_url/",
"program_guide":"https://example_url/",
"electronic_program_guide":"https://example_url/",
"catchup_tv":"7",
"popup":{
"type":"blocking",
"message":"Temporary Down",
"buttons":{
"button":{
"type":"cancel",
"text":"OK"
}
}
},
"category_id":"12797",
"path":"Smart TV App>Live TV>Pakistani>Entertainment"
}
]
}
}
}

var data = {
"xml": {....
var answer = data.xml.items.item
.map(x => ({ name: x.name, channel_remote_number: x.channel_remote_number }));

Related

ES6 : Create objects in a given format from array of key values

var result = {445: "L005.0", 455: "L006.0", 456: "L007.0", 457: "L008.0", 458: "L009.0", 459: "L027.0", 467: "L005.7", 580: "L001.0", 581: "L002.0", 587: "L003.0"};
From this "result", I want to output an object like this
{
"445": {
name: result[445],
icon: "fa-search"
},
"455": {
name: result[455],
icon: "fa-search"
},
"456": { ... },
"457": { ... },
...
...
}
So you need to iterate over keys to construct new object o;
let res={}; //initializing
for(let i of Object.keys(result))
{
res[i]={
"name": result[i],
"icon":"fa-seach"
}
}
console.log(res) //to test

How to mapping variable data for pie chart (react.js)

I want make pie chart using state value in react with '#toast-ui/react-chart'.
I tried this and that after looking at the examples, but it's hard to me.
This is a example.
//chart data
var data = {
categories: ['June, 2015'],
series: [
{
name: 'Budget',
data: [5000]
},
{
name: 'Income',
data: [8000]
},
{
name: 'Expenses',
data: [4000]
},
{
name: 'Debt',
data: [6000]
}
]
};
var options = {
chart: {
width: 660,
height: 560,
title: 'Today's Channel & Value.'
}
tooltip: {
suffix: 'value'
}
},
};
var theme = {
series: {
colors: [
'#83b14e', '#458a3f', '#295ba0', '#2a4175', '#289399',
'#289399', '#617178', '#8a9a9a', '#516f7d', '#dddddd'
]
}
};
//render part
render()
{
return(
<div>
<PieChart
data={data}
options={options}
/>
</div>
}
and document is here.
https://github.com/nhn/toast-ui.react-chart#props
https://nhn.github.io/tui.chart/latest/tutorial-example07-01-pie-chart-basic
What's in the document is how to make a chart with a fixed number, but I want to change it using the state.
So, How can I mapping series data like this and how to add data length flexible?
I have list of object like ...
this.state.list =[{"channel_name":"A","channel_number":17,"VALUE":3,"num":1},
{"channel_name":"B","channel_number":23,"VALUE":1,"num":2},
{"channel_name":"C","channel_number":20,"VALUE":1,"num":3},
{"channel_name":"D","channel_number":1,"VALUE":1,"num":4}]
The length of the list is between 1 and 7 depending on the results of the query.
I want to do like this.
series:[
{
name: this.state.list[0].channel_name+this.state.list[0].channel_num
data: this.state.list[0].VALUE
},
{
name: this.state.list[1].channel_name+this.state.list[1].channel_num
data: this.state.list[1].VALUE
},
{
name: this.state.list[2].channel_name+this.state.list[2].channel_num
data: this.state.list[2].VALUE
},
{
name: this.state.list[3].channel_name+this.state.list[3].channel_num
data: this.state.list[3].VALUE
}
]
How can I implement it however I want?
Since this.state.list is a list of objects, so you can simply use map method to loop through each object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map. Then create new object with custom value.
let new_series = [];
this.state.list.map((obj) => {
let info = {
name: obj.channel_name + obj.channel_number, //from your code
data: obj.VALUE //from your code
}
new_series.push(info)
});
Then assign new list to your chart.
//chart data
var data = {
categories: ['June, 2015'],
series: new_series
]
};

React JS: Filter an array by a string

it's me again, now with reactjs.
I have a json, inside it have two "rows", they are Description and ubication. I need to filter the array by Description.
How can I filter this? The description it's in text format for example "Impact" or "Wiu wiu".
I know the function filter() of typescript have a numeric condition but no have a text condition or I haven't seen it been use for that.
Thank you so much for your help.
No still you can do with Text as well,
let filtered = yourArray.filter(t=>t.Description ==='impact');
DEMO
let mydata = {
"ArrayBotones": [
{
"descripcion": "Impacto",
"ubicacion": "sonidos/impacto.mp3"
},
{
"descripcion": "Soy Fede",
"ubicacion": "sonidos/holasoyfede.wav"
},
{
"descripcion": "Wiu wiu",
"ubicacion": "sonidos/wiuwiu.wav"
},
{
"descripcion": "3 carajos",
"ubicacion": "sonidos/3carajos.wav"
},
{
"descripcion": "Apurate Jose",
"ubicacion": "sonidos/apuratejose.wav"
},
{
"descripcion": "No, no se",
"ubicacion": "sonidos/nonose.wav"
}
]
};
let result = mydata.ArrayBotones.filter(t=>t.descripcion === 'Impacto');
console.log(result);

Retrieve a child that is inside another child to an array in Firebase

Here is the structure of data:
I am trying to retrieve "guestName" and "profilePicture" of the guestList to an array (this.guestList).
I am getting a snapshot using this code:
this.itemProvider.getguestList(this.navParams.get('itemId'))
.on('value', guestSnapshot => {
guestSnapshot.forEach(snap => {
this.guestList.push({
id: snap.key,
guest: snap.val()
});
return false;
});
I am getting the array like this:
console.log:
eventList​ length​ ​3​ [
{
"id": "-L1zPnV8eFpyTZlRZSkS",
"guestList": {
"profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zPnV8eFpyTZlRZSkS%2FItemPicture.png?alt=media&token=974cf1c7-974b-4f10-89bd-9033ef25d476",
"​guest​Name": "Test"
}
},
{
"id": "-L1zQ41CVIwKTbAA_yiQ",
"​guestList": {
"​profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zQ41CVIwKTbAA_yiQ%2FItemPicture.png?alt=media&token=bd32f971-7b81-4891-8f1f-9fd47185d105",
"​guest​Name": "Test"
}
},
{
"id": "-L1zQKQBrnpgVdwqIdsU",
"guestList": {
"profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zQKQBrnpgVdwqIdsU%2FItemPicture.png?alt=media&token=ab7d193c-8938-4820-aa8d-d091c9d89211",
"​guest​Name": "Test"
}
}
]
I need to rich "profilePicture" and guestName to be the members of the array.
What do I need to change?
You can either sub-address the snapshot or sub-address the value.
Sub-addressing in the snapshot is done with child():
guestSnapshot.forEach(snap => {
this.guestList.push({
id: snap.key,
guest: snap.child('"guestlist").val()
});
Sub-addressing in the value is done with:
guestSnapshot.forEach(snap => {
this.guestList.push({
id: snap.key,
guest: snap.val().guestlist
});

$push in MongoDb not working?

my schema looks like this:
var exampleSchema = newSchema({
profile:{
experience :[{
exp : String
}]
}
});
this is the codes to update experience in profile collection:
exampleSchema.statics.experience = function (id,experience, callback){
var update = {
$push: {
'profile.experience': experience
}
}
this.findByIdAndUpdate(id,update,function(err) {
if (err) {
callback(err);
} else {
callback(null);
}
})
I was getting error like The field 'profile.experience' must be an array but is of type String in document {_id: ObjectId('5653f1d852cf7b4c0bfeb54a')}[object Object]
console.log(experience) is equal to
{ exp: 'jlkjlkjlk' }
my collection should look like this:
experience:[
{
exp : "YYYY"
},
{
exp:"xxxx"}
]
Imagine that you have this collection:
/* 1 */
{
"_id" : ObjectId("565425e862760dfe14339ba8"),
"profile" : {
"experience" : [
{
"exp" : "Experto"
}
]
}
}
/* 2 */
{
"_id" : ObjectId("565425f562760dfe14339ba9"),
"profile" : {
"experience" : {
"exp" : "Experto"
}
}
}
/* 3 */
{
"_id" : ObjectId("5654260662760dfe14339baa"),
"profile" : {
"experience" : "Experto"
}
}
If you try (update doc /* 2 */):
db.profile.update(
{ _id: ObjectId("565425f562760dfe14339ba9") },
{ $push: { "profile.experience" : { exp : "Intermediate" } } }
)
You get this error:
The field 'profile.experience' must be an array but is of type Object
in document {_id: ObjectId('565425f562760dfe14339ba9')}
And if you try (update doc /* 3 */):
db.profile.update(
{ _id: ObjectId("5654260662760dfe14339baa") },
{ $push: { "profile.experience" : { exp : "Intermediate" } } }
)
You will get:
The field 'profile.experience' must be an array but is of type String
in document {_id: ObjectId('5654260662760dfe14339baa')}
i changed Schema like this
experience : [{type:String,exp:String}],
my update object looks like this
var update = {
$push: {
'profile.experience': san.exp
}
};
san looks like this :{ exp: 'YYY' }
Inside mongoose collectionlooks like this used RoboMongo
"experience" : [
"experienced in XXX",
"YYY"
],
$push: {
'profile.experience': experience
}
Remove .exp.
First you have to check you declared your field as an array like this(look at field products):
shop = {
'name': "Apple Store",
'description': "",
'direction': "",
'contact': "",
'products':[]
}
Now if you want to add something to the field products using $push
product = {
'name': "Iphone 6",
'description': "Iphone model 6, 64GB",
'price': 700,
'count': 3
}
myquery = { "name" : "Apple Store" }
obj ={"$push":{"products":{"$each": [product]}}}
db.collection.update_one(myquery,obj)
This code is provided for PyMongo framework. To use in MongoDB directly replace update_one by update. Mongo resource
You may use $set instead of $push which might work.
$set: {
'profile.experience': experience
}
are you searching for adding multiple values into single field then use this one.
write this one your model or schema:
arrayremarks:[{remark: String}]
then write in your controller:
module.exports.addingremarks = (req, res) => {
let casenum=JSON.parse(JSON.stringify(req.body.casenum).replace(/"\s+|\s+"/g,'"'))
var rem={remark:"Suman macha"}
Inwart.update( { 'casenum': casenum },{ $push: { arrayremarks:rem} } ,function (err, inwarts) {
if (err)
return console.error(err);
res.send(inwarts);
}
)
}

Resources