Split the array and get value [React native] - arrays

I am developing the react native application and I want to split the array but I can't understand how to do that,
[{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
I have something like the above array, I want to get the value of the inTagId and also last integer value "3", "6", "7", "8" from this array

var a = [{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
var tagID = a[0].inTagId
var b = Object.keys(a)
var lastInteger = b[(b.length-1)]

var test = [{"dtCreatedOn": "2021-06-01T03:26:44.910Z", "flgIsActive": true, "inTagId": 1, "stTags": "Emotion"},"4","5","6"]
var tags = [];
test.map(function(ele) {
if(typeof ele === 'object')
{
tags.push(ele.inTagId);
}
else
{
tags.push(ele)
}
});
tags.join(',')

Related

Assigning values to every other index in an array

I have three arrays that correlate to one another (Users, Wins, and Lost), Wins[0] through Wins[2] stand for Users[0] through Users[2]. If Users[0] through Users[2] won and Users[3] as well as Users[4] lost, then Lost[3] and Lost[4] need to be equal to 1.
var Users = ["user1", "user2", "user3", "user4", "user5"];
var Wins = ["1", "1", "1", "0", "0"];
var Lost = ["0", "0", "0", "0", "0"]; //Lost[3] and Lost[4] need to be equal to 1
You can simply use map and a ternary operator to produce the Lost array:
var Users = ["user1", "user2", "user3", "user4", "user5"];
var Wins = ["1", "1", "1", "0", "0"];
var Lost = Wins.map((win) => win === "1" ? "0" : "1");
console.log(Lost);
const lost = wins.map(w => w === "1" ? "0" : "1");

how to use for loop make an array which contains something like a dictionary

I want to create an array something like this
array=["1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8"]
It's something like a dictionary, every value needs to be a key:value pair,
so my problem is how to init this type of array? The following is my work, it doesn't work.
array=[String:String]()
for i in 0...7{
array.append(String(i):String(i))
}
every line has a bug!!
plz help
Its not something like Dictionary it is Dictionary, if you want to make a dictionary you can go like this way.
var dictionary = [String:String]()
for i in 0...7{
dictionary[String(i)] = String(i)
}
print(dictionary)
["0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7"]
In addition to Nirav D's answer; I feel the following may help:
There is a method updateValue(_:​for​Key:​) that updates (or adds new key-value pair if the key does not exist) the value for given key.
So your code would look like this:
var array = [String:String]()
for i in 0...7 {
array.updateValue(String(i), forKey: String(i))
}
print(array)
// Output
["2": "2", "1": "1", "6": "6", "4": "4", "3": "3", "7": "7", "0": "0", "5": "5"]
Swift is always amazing you can define += operator that make it easier. So the code will be as follows:
// Defining += operator
func += <K, V> (inout left: [K:V], right: [K:V]) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}
// Usage
var array = [String:String]()
for i in 0...7{
array += [String(i):String(i)]
}
print(array)
// Output
["2": "2", "1": "1", "6": "6", "4": "4", "3": "3", "7": "7", "0": "0", "5": "5"]

How to use array include in multidimension array angular 2

hi this is my array
this.testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
i want to check whether officename sun is there or not. I tried used include
this.testArray.includes("sun")
but this will work for one dimension array( it will return true). how if I want to use it for this multidimension array. it always return false.
** this.testArray.includes("sun") This will work with this array**
this.testArray = ["sun","moon","stars" }];
it will return true it exist. but it is not working with my array the up one.
Your array is not multidimensional. However you can do it like :
testArray.find(function(i){
return (i.officename === 'sun');
});
In case you need a filter directly
| filter:{officename:'sun'}
or negative
| filter:{officename:'!sun'}
Simply you can check value include or not by using underscorejs find function
this.testArra = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }]
var temp = _.find(this.testArra, function (o) { return o.officename==='sun2'; })
if temp "undefined" value not include, else value include
you can use find method in javascript
ES6 demo
var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
var arr = testArray.find(o=> o.officename === "sun");
console.log(arr)
ES5 demo
var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];
var arr = testArray.find(function(o){
return o.officename === "sun"
});
console.log(arr)

Combining two arrays into value pairs to make a full deck of cards

I am trying to combine two two arrays to make a full deck of cards that looks like so:
[{card: "A", suit: "d"}, {card: "A", suit: "c"}, {card: "A", suit: "s"}, {card: "A", suit: "h"}, {card: "2", suit: "d"}.....]
.... this is what I have so far:
function newDeck(ranks, suits){
var ranks = [ "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
var suits = ["d", "c", "s", "h"]
var deck= []
for (i = 0; i < suits.length; i++) {
for (j = 0; j < ranks.length; j++) {
this.deck[ranks.length*i+j] = new Card(ranks[j], suits[i]);
}
} console.log(newDeck)
}
Using Array.forEach you can do the following:
var ranks = [ "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var suits = ["d", "c", "s", "h"];
var deck= [];
suits.forEach(function(suit){
ranks.forEach(function(rank){
deck.push(new Card(rank, suit));
})
});
EDIT: and in case you haven't written the Card method yet:
function Card(rank, suit){
this.card = rank;
this.suit = suit;
}
If you combine the two arrays you will have an array:
["A","2","3","4","5","6","7","8","9","10","J","Q","K","d","c","s","h"]
which does not represent a full deck of card, whereas using an embedded for loop to print out card as you are now will so I don't think you just want to append one array to the other. Can you provide more context on what you want to do with the array?
However, to answer your question: if you want to append two arrays you can use:
var appendedArray = ranks.concat(suits);
which will result in the aforementioned array above
pertaining to your updated question: you are called "new Card(ranks[j], suits[i]);" have you made a Card constructor so that this is valid? if so, the code should be correct if the constructor matches how you are using it. Posting the code for the constructor would be helpful along with an update of what issue you are facing

How to create an array of dictionaries?

new to programming!
I'm trying to create an array of dictionaries inside a struct in Swift like so:
var dictionaryA = [
"a": "1",
"b": "2",
"c": "3",
]
var dictionaryB = [
"a": "4",
"b": "5",
"c": "6",
]
var myArray = [[ : ]]
myArray.append(dictionaryA)
myArray.append(dictionaryB)
This works fine in a playground, but when I put it into an Xcode project, inside a struct, the lines with the append function produce the error "Expected declaration".
I've also tried using the += operator with the same result.
How can I successfully construct this array inside the struct?
From your error Expected declaration, I assume you are doing like:
struct Foo {
var dictionaryA = [
"a": "1",
"b": "2",
"c": "3",
]
var dictionaryB = [
"a": "4",
"b": "5",
"c": "6",
]
var myArray = [[ : ]]
myArray.append(dictionaryA) // < [!] Expected declaration
myArray.append(dictionaryB)
}
This is because you can place only "declarations" in the struct body, and myArray.append(dictionaryA) is not a declaration.
You should do that somewhere else, for example in the initializer. The following code compiles.
struct Foo {
var dictionaryA = [
"a": "1",
"b": "2",
"c": "3",
]
var dictionaryB = [
"a": "4",
"b": "5",
"c": "6",
]
var myArray = [[ : ]]
init() {
myArray.append(dictionaryA)
myArray.append(dictionaryB)
}
}
But as #AirspeedVelocity mentioned, you should provides more information about myArray, or myArray would be Array<NSDictionary> which I think you don't expect.
Anyway, the correct solution would vary depending on what you really trying to do:
Maybe or maybe not, what you want is something like:
struct Foo {
static var dictionaryA = [
"a": "1",
"b": "2",
"c": "3",
]
static var dictionaryB = [
"a": "4",
"b": "5",
"c": "6",
]
var myArray = [dictionaryA, dictionaryB]
}
But, I don't know, why don't you just:
struct Foo {
var myArray = [
[
"a": "1",
"b": "2",
"c": "3",
],
[
"a": "4",
"b": "5",
"c": "6",
]
]
}
The problem lies with this line:
var myArray = [[ : ]]
You need to tell Swift what type myArray is – [[:]] isn’t enough information.
You can either do it the explicit way:
var myArray: [[String:String]] = [[ : ]]
Or, if practical, implicitly using the first or both values you plan to put in:
var myArray = [dictionaryA]
var myArray = [dictionaryA,dictionaryB]
(as an alternative to the explicit empty version, you can also write var myArray = [[String:String]](), which is shorthand for var myArray = Array<Dictionary<String,String>>())
var arrayOfDict = [[String: Int]]()
// Create a dictionary and add it to the array.
var dict1: [String: Int] = ["age": 20]
arrayOfDict.append(dict1)
// Create another dictionary.
var dict2: [String: Int] = ["rank": 5].
arrayOfDict.append(dict2)
// Get value from dictionary in array element 0.
if let value = arrayOfDict[0]["age"] {
print(value)
}
Output
20
Or you can use an array of tuples that´s even easier, like this:
var myArray:[(a:String,b:String,c:String)] = []
And append any element you need later:
self.myArray.append((a:"A",b:"B",c:"c"))
And to use them just:
self.myArray[index].a
self.myArray[index].b
self.myArray[index].c

Resources