How to generate a new object array from another 2 arrays [closed] - reactjs

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have 2 arrays like and , I want to generate a new array with 2 elements like pID and cNo. pID is getting from the first array and the cNO is from the second array. How to merge these 2 arrays like
newarr= { 0: pID:970989, cID:'dfds'
1: pID:970995, cID:'fgd',
2: pID:971006, cID:'t765'}

Assuming your arrays are equal in length you can just loop over one of them and create a new array.
const newArray = pIDArray.map((pID, index) => ({ pID, CID: cIDArray[index] }));

Related

Ruby, how to swap the elements in an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How do I swap an(example) array's second and third elements? I tried the following
def swap_elements(array)
array = ["blake", "ashley", "scott"]
array[1], array[2] = array[2], array[1]
end
but I get
: ["scott", "ashley"]
I lost the first[0] element
There are many ways to do this - this is a functional approach that doesn't mutate the original array:
def swap_elements(array)
# yields the array to the block
array.then do |first, *rest| # deconstruct the array
rest.reverse # swap the places of 2 & 3
.unshift(first) # put the first back in
end
end

MATLAB Basics - interpreting parentheses and colons in array manipulation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
What does it mean in MATLAB when I do:
array = array (1:number)
and what does it mean:
array = array(indexes_array)
and finally, what does it mean:
array = array(indexes_array,:)
Answers, according to MATLAB docs:
1)
array = array (1:number)
The colon notation in MATLAB provides an easy way to extract a range of elements from v:
v(3:7) % Extract the third through the seventh elements
ans =
9 4 2 11 7
2)
array = array(indexes_array)
array is reorganized according to indexes_array order, assuming that indexes_array is composed of indexes.
3)
array = array(indexes_array,:)
just the same as number 1), array is reorganized according to all the rows of indexes_array, ignoring the columns.
References:
https://www.mathworks.com/help/matlab/math/array-indexing.html
https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

Ruby : How to compare 2 arrays of string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 2 arrays of strings as under. I want to compare them and execute some code if these arrays are not equal-
current_instances = ["170601_7711", "170601_8811"]
app_instances = ["170602_7711", "170602_8811"]
How can I compare them in ruby?
x = ["alpha1", "beta1"]
y = ["alpha2", "beta2"]
Check if all elements are the same
x == y #=> false
Compare each element
Compare each string at some index i, assuming both arrays are the same size. Then apply some code to the matches.
x.zip(y) #=> [["alpha1", "alpha2"], ["beta1", "beta2"]]
x.zip(y).map {|a,b| a == b ? 'do this' : 'else do this' }
#=> ["else do this", "else do this"]
Perhaps you can do an array difference with the - operator and execute your code if the difference is zero
arr_diff = current_instances - app_instances
This is the simplest solution I could think of :)
Check my solution and let me know how it goes

Print random element from array with Lua [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What i want to do,is print random elements from array,heres example code:
myTable = { "one", "two", "three","four"}
-- print here: one,three,four
Printing random element is simple -- print(myTable[math.random(#myTable)]) -- but if you need to make each printed element unique, then you better shuffle the elements in the array and print the resulting elements one-by-one. You may check this SO answer for ideas.
If you want N amount of elements, you need to make use of a loop:
local myTable = { "one", "two", "three","four"}
local result = {}
for i=1,3 do -- N here, e.g 3 if you want 3 elements
result[i] = table.remove(myTable,math.random(#myTable))
end
print(table.concat(result,", "))
-- "four, two, three" as an example
The code will error if you request more elements than there are in the table. If you want to reuse the table later on, you'll have to copy it, as this code actually removes elements from the table.

Finding a value in an array of class objects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to find a value of in an array of class objects. Then once it is found to be included, can you find what place within the array it resides at? E.g.
class SomeClass
def initialize(value,otherData)
#value = value
#otherData = otherData
end
end
x = 0
otherData = "foo"
list = Array.new
while (x < 3)
list.push SomeClass.new(x,otherData)
x += 1
end
I want to find list[x].value where value=2 and figure out what place in the array that is located at.
If SomeClass had a value method, an obvious way would be:
ix = list.find_index { |sc| sc.value == 2 }

Resources