How to Combine Object Values from Separate Lines into One Line

How to combine object values from separate lines into one line

I think, this could be your solution:

var carData = [    {date:"2018-06-08 13:20:00", type:"OUT", car:"Car1", user:"User1"},    {date:"2018-06-08 14:13:00", type:"IN", car:"Car1", user:"User1"},    {date:"2018-06-08 14:20:00", type:"OUT", car:"Car2", user:"User2"},    {date:"2018-06-08 14:20:00", type:"OUT", car:"Car3", user:"User4"},    {date:"2018-06-08 14:35:00", type:"IN", car:"Car2", user:"User2"},    {date:"2018-06-09 11:12:00", type:"OUT", car:"Car1", user:"User1"},    {date:"2018-06-09 12:13:00", type:"IN", car:"Car1", user:"User1"},    {date:"2018-06-10 17:12:00", type:"OUT", car:"Car1", user:"User3"},    {date:"2018-06-10 18:13:00", type:"IN", car:"Car1", user:"User3"},    {date:"2018-06-10 19:12:00", type:"OUT", car:"Car2", user:"User1"},    {date:"2018-06-10 20:13:00", type:"IN", car:"Car2", user:"User1"}];
var carDataCombined = [];
function addOneHour(date){ date = new Date(date); date.setHours(date.getHours() + 1); var hours = (date.getHours() < 10) ? '0' + date.getHours() : date.getHours(); var minutes = (date.getMinutes() < 10) ? '0' + date.getMinutes() : date.getMinutes(); var seconds = (date.getSeconds() < 10) ? '0' + date.getSeconds() : date.getSeconds(); return date.toISOString().substring(0, 10) + ' ' + hours + ':' + minutes + ':' + seconds;}
for(var i = 0; i < carData.length; i++){ if(carData[i].type === 'OUT'){ var dateIn; if(carData[i+1] && carData[i+1].type === 'IN' && (carData[i].car === carData[i+1].car && carData[i].user === carData[i+1].user)){ dateIn = carData[i+1].date; } else { dateIn = addOneHour(carData[i].date) } carDataCombined.push({dateOut: carData[i].date, dateIn: dateIn, car: carData[i].car, user: carData[i].user}); }}
console.log(carDataCombined)

Is there a way to combine those two lines into one?

No, you can't do it the way your are right now. You're abusing destructuring which is supposed to be used to take fields from an object and turn them into local variables. You're doing this and then immediately converting them back into an object.

To do this in one, step, simply make a new object and reference the fields you want from the old object.

const pagination = { page: this.props.page, totalPages: this.props.totalPages, changePage: this.props.changePage }

Merge multiple lines from for loop into one list

You could concatenate all rows to a single string variable:

res = ""
for message in data['messages']:
splittext= message['agentText'].strip().replace('\n',' ').replace('\r',' ')
if len(splittext)>0:
res += splittext + " "

Or alterantively use string methods with the help of a list:

res = []
for message in data['messages']:
splittext= message['agentText'].strip().replace('\n',' ').replace('\r',' ')
if len(splittext)>0:
res.append(splittext)
print(" ".join(res))

Javascript - Return object's values on separate lines in single string

What about

Object.values(parent).join("\n");

Combine and print different variables on one line

Simply replace

user = open('user.txt', 'r').readlines()
color = open('color.txt', 'r').readlines()
number = open('number.txt', 'r').readlines()

with

user = open('user.txt', 'r').read().splitlines()
color = open('color.txt', 'r').read().splitlines()
number = open('number.txt', 'r').read().splitlines()

and replace generated = u + c + n with generated = u + c + n + "\n".

The str.splitlines() method removes the \n characters that would cause the newlines.

But do note that is is better practice to use the with keyword to open your files:

with open('user.txt') as u, open('color.txt') as c, open('number.txt') as n:
user = u.read().splitlines()
color = c.read().splitlines()
number = n.read().splitlines()

SVG : how to merge multiple lines to obtain single object

Hint 1

Your first path

M 129.734598 192.711157 V 226.160594

is equivalent to

M 129.734598 192.711157 L 129.734598 226.160594

Your second path

M 129.734598 192.711157 H 159.496439

is equivalent to

M 129.734598 192.711157 L 159.496439 192.711157

Perhaps in this form, the sequence of moves might be more obvious.

Hint 2

In your first attempt you were close (I've rounded these values for more clarity)

M 129 192 V 226 H 159 V 226 H 159

Your last two path commands are not doing anything.

In order to complete the square, your third side (the second V) needs to return to the start Y position. And your fourth side (the second H) needs to return to the start X position.

Hope this helps, and is not too cryptic.

how to combine multiple lines from multiple files and put them to an array

When you iterate of a File object, you can only iterate on it once.
When the 3 lines of z are read, the y for loop goes to the next line in f2. However the iteration ends since there is no other line to read in f3.

One solution would be to re-open the files at all iterations, but that's not very sexy. I suggest reading the three files in the opening call directly.

My version :

list1 = []
lines = []
for file in ['f1', 'f2', 'f3']:
with open(file) as f:
lines.append(f.readlines())
for xline in lines[0]:
for yline in lines[1]:
for zline in lines[2]:
list1.append((xline.strip(), yline.strip(), zline.strip()))

Combining multiple lines into one single line

You actually do not need to replace the line; Because you are technically recalling line by line.

All you need to do is create a StringBuilder to which you append every line from your buffered reader.

Your code should look like this

String sCurrentLine;

StringBuilder builder = new StringBuilder();
while ((sCurrentLine = br.readLine()) != null){
builder.append(sCurrentLine);
}

You can now output the content of the "builder" to another file.



Related Topics



Leave a reply



Submit